FileUpload control file Upload, expansion, limit file types and upload verification

Source: Internet
Author: User

1. Upload the selected file to the target location:

<span style="White-space:pre"> </span>//get the file name of the uploaded file            stringName =Fileupload1.filename; //stitching the file name into the desired relative path            stringPath ="images/"+ DateTime.Now.ToString ("YYYYMMDDHHMMSS") +name; //perform the Save As method, but the absolute path is required here, then use the path mappingFileupload1.saveas (Server.MapPath (path));

2, upload file default size limit is 4MB, can be expanded, the way is:

<span style="White-space:pre"> </span>//Add the following code to the system.web tag in the Web. config configuration file, with a maximum length of 4096, in kilobytes, and 10 times times the expansion below<span style="White-space:pre"> </span><system.web> <span style="White-space:pre"> </span> "40960"/> <span style="White-space:pre"> </span></system.web>

3. Limit the types of browse files:

    <span style="white-space:pre">    </span><!--restrict the type of browse file, add the Accept attribute-- >              <asp:fileupload id="FileUpload1" runat="server" accept=". Png,.jpg,.jpeg" />  

4. Upload verification:

Divided into two ways, server-side validation will refresh the page, the client using JS authentication will not refresh the page, we recommend the use of JS authentication.

Service side:

    <span style="white-space:pre">    </span>//  The length of the uploaded file can be obtained using the  b            if (FileUpload1.PostedFile.ContentLength > (4  (1024x768)              {                  " file exceeds 4m! ";                   return ;              }  

Client:

<script type="Text/javascript">window.onload=function () {//Upload button click eventdocument.getElementById ('BTN1'). onclick =function () {//fetch to file element                    varFL1 = document.getElementById ('file1'); //determine if a file is selected                    if(Fl1.value.length <=0) {alert ('Please select a file to upload! '); return false; }                      //get the file length and Judge                    if(fl1.files[0].size > (4*1024x768*1024x768) {alert ('file exceeds 4m! '); return false; }  
<span style="White-space:pre"> </span>//Verify that the end extension is correct                    if(Fl1.value.substr (fl1.value.length-4)!='. jpg'|| Fl1.value.substr (fl1.value.length-5)!='. JPEG'|| Fl1.value.substr (fl1.value.length-4)!='. PNG') {alert ('the selection is not a picture file! '); return false;              }                  };          }; </script>

FileUpload control file Upload, expansion, limit file types and upload verification

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.