Before uploading images to determine file format and size Verify that the file is not a picture

Source: Internet
Author: User
Tags bool config file size

  Since the upload is a picture, then you should upload the file before the verification, to see if the upload is not a picture file

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:  code as follows://File type:  protected bool Isallowablefiletype (string FileName)   {  //read from Web.config file type restriction   string stringstrfiletypelimit;  Stringstrfiletypelimit = configurationmanager.appsettings["Picturetye"]. ToString (); ///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 has exceeded the maximum limit:  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"]);   // Determine if the file exceeds the limit   if (doubleifIlesizelimit > Filecontentlength)   {  return true; }  else  {  return false; }&NB Sp }    is the configuration file settings, which specify the extension and size of the uploaded file.     Code is as follows: <appSettings>  <add key= "Picturetye" value= ". Jpg|. Gif|. Png|. bmp|. Jpeg| " />  <add key= "Filesizelimit" value= "512000"/>  </appSettings>    code is 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.