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