Recent work needs to do a upload image function, since the upload is a picture, then should be uploaded before the file to verify that you want to upload is not a picture file. So verify the format of the selected file before uploading. Upload the picture because the server resources are limited, often will stipulate the maximum number of pictures, so before uploading to the picture size of a verification. Let's do these two validations today.
Verify the file type to see if the selected file is a picture:
Copy Code code as follows:
File type:
protected bool Isallowablefiletype (string FileName)
{
Read from web.config to determine file type limits
String Stringstrfiletypelimit;
Stringstrfiletypelimit = configurationmanager.appsettings["Picturetye"]. ToString ();
Whether the current file name extension is contained in this string
Response.Write (FileName + stringstrfiletypelimit);
if (Stringstrfiletypelimit.indexof (Filename.tolower ())!=-1)
{
return true;
}
Else
{
return false;
}
}
Verify the file size to see if the file exceeds the maximum limit:
Copy Code code as follows:
File size
public bool Isallowablefilesize (long Filecontentlength)
{
Read from web.config to determine file size limitations
Int32 Doubleifilesizelimit;
Doubleifilesizelimit = Convert.ToInt32 (configurationmanager.appsettings["Filesizelimit"));
To determine if a file exceeds the limit
if (Doubleifilesizelimit > Filecontentlength)
{
return true;
}
Else
{
return false;
}
}
Here is the configuration file settings, which specify the extension and size of the uploaded file.
Copy Code code as follows:
<appSettings>
<add key= "Picturetye" value= ". Jpg|. Gif|. Png|. bmp|. Jpeg| " />
<add key= "Filesizelimit" value= "512000"/>
</appSettings>
Code is very simple, as long as the two methods before uploading, you can upload the file for a simple verification, not only applicable to the image upload, other documents, as long as you need to modify the configuration file.