Before uploading images to determine file format and size verification file is not a picture _ practical skills

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.