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.