Determine the file format and size before uploading images

Source: Internet
Author: User

In recent work, you need to upload an image. Since the image is uploaded, you should verify the file before uploading to see if it is an image file to be uploaded. Therefore, you must verify the format of the selected file before uploading. When uploading images, due to limited server resources, the maximum number of images is usually limited. Therefore, you must verify the image size before uploading images. Let's do the two verification today.


Verify the file type to see if the selected file is an image:

 //  File Type:  Protected   Bool Isallowablefiletype ( String  Filename ){  //  Read from web. config to determine the file type Limit     String  Stringstrfiletypelimit; stringstrfiletypelimit = Configurationmanager. appsettings [ "  Picturetye  "  ]. Tostring ();  //  Whether the current file extension is included 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:

 

 //  File Size  Public   Bool Isallowablefilesize ( Long Filecontentlength ){  //  Read from web. config to determine the file size limit  Int32 doubleifilesizelimit; doubleifilesizelimit = Convert. toint32 (configurationmanager. etettings [ "  Filesizelimit  "  ]);  //  Determine if the file exceeds the limit      If (Doubleifilesizelimit> Filecontentlength ){  Return  True  ;}  Else  {  Return   False  ;}} 

 

 

 

The following is the configuration file settings, which specifies the extension and size of the uploaded file.

<Appsettings><AddKey= "Picturetye"Value= ". Jpg |. gif |. PNG |. BMP |. JPEG |"/><AddKey= "Filesizelimit"Value= "512000"/></Appsettings>

 

 

CodeYou can call these two methods before uploading a file. You can perform a simple verification on the file to be uploaded. This method is not only applicable to image uploading, but also to other files, modify the configuration file as needed.

 

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.