Webform (File upload)

Source: Internet
Author: User
Tags server memory

1.HTML Encoding:

<type= "file"/>

2. Control: FileUpload

It is used to select files to upload, and a button is required to upload the selected files to the server

String path = "images/" + fileupload1.filename;
Fileupload1.saveas (Server.MapPath (path));

Optimization 1: The file retains its original name and suffix

String path = "images/" + fileupload1.filename;

Optimization 2: Prevent duplicate names, or multiple people at the same time upload the same name file

String path = "images/" + DateTime.Now.ToString ("Yyyymmddhhssmmms") + request.cookies["Ures"]. Value + fileupload1.filename;

Optimization 3: Can upload large files, default is 4MB,4096KB
Method: Expansion

Add the following sentence to the system.web tag in Web. config:

Note: Do not expand too much, because if multiple people upload large files at the same time, it may cause server memory overflow, causing the server to crash.

Optimization 4: Exceeding the size of the upload request, preventing uploading and prompting the file to be too large
C # Side resolution:

if (Fileupload1.postedfile.contentlength> (1024x768*)) {Labei1.text="  file More than 10M, do not upload such a large! "; return ;}

C # End is bad, because if the file exceeds the maximum length, the C # end is limited, and the program crashes directly

JS End:

document.getElementById ("Button1"). onclick =function () {        //Remove the upload element        varFI1 = document.getElementById ("FileUpload1"); //determine if there is a selected file        if(fi1.value.length <= 0) {alert (' Please select the file you want to upload! ‘); return false; }        Else {            //Verify that the selected file length meets the criteria            if(Fi1.files[0].size > (1024 * 1024 * 10) {alert (' File too large, not allowed to upload! ‘); return false; }        }    };

Optimization 5: Limit the types of files that can be selected

In the FileUpload property, add accept= ". Jpg,.jpeg,.png"

Breakpoint Continuation:

1 usingSystem;2 usingSystem.Data;3 usingSystem.Configuration;4 usingSystem.Collections;5 usingsystem.web;6 usingSystem.Web.Security;7 usingSystem.Web.UI;8 usingSystem.Web.UI.WebControls;9 usingSystem.Web.UI.WebControls.WebParts;Ten usingSystem.Web.UI.HtmlControls; One usingSystem.IO; A  Public Partial classDFile:System.Web.UI.Page - {  - protected voidPage_Load (Objectsender, EventArgs e) the {  - }  - protected voidLinbtndfile_click (Objectsender, EventArgs e) - {  + //create a bit array - byte[] buffer =Newbyte[10240];  + //specifies the path to the file to download. A stringFilePath =@"d:\ Love-wise nook. rar";  at //or take the file name including the extension - stringFileName =Path.getfilename (filePath); -Stream FileStream =NULL;  - Try  - {  - //Open File inFileStream =NewFileStream (FilePath, FileMode.Open, FileAccess.Read, fileshare.read); - response.clear (); to //get the size of a file + LongFileSize =filestream.length; - Longsum =0;  the if(request.headers["Range"] !=NULL)  * {  $Response.statuscode =206;//An integer that represents the HTTP output status returned to the client. The default value is 200. Panax Notoginsengsum =Long. Parse (request.headers["Range"]. Replace ("bytes=",""). Replace ("-",""));  - }  the if(Sum! =0)  + {  AResponse.AddHeader ("Content-range","bytes"+ Sum. ToString () +"-"+ ((Long) (FileSize)). ToString () +"/"+filesize.tostring ());  the }  + //Get partial HTTP header information -Response.AddHeader ("Content-length", ((Long) (FileSize-sum)). ToString ());  $Response.ContentType ="Application/octet-stream";  $ //Get File Source -Response.AddHeader ("content-disposition","attachment; Filename="+Httputility.urlencode (Request.ContentEncoding.GetBytes (fileName));  - //Response.Flush (); thefilestream.position = sum;//set Current stream position -FileSize = FileSize-sum;Wuyi //when the file size is greater than 0, it enters the loop the  while(FileSize >0)  - {  Wu //determine if the client is still connected to the server - if(response.isclientconnected) About {  $ //gets the total number of bytes in the buffer. - intLength = filestream.read (buffer,0,10240);  - //Write Data -Response.OutputStream.Write (Buffer,0, length);  A //send the output of the buffer to the client + Response.Flush (); theBuffer =Newbyte[10240];  -FileSize = FileSize-length; $ }  the Else  the {  the //exit loop when user disconnects theFileSize =-1;  - }  in }  the }  the Catch(Exception ex) About {  theResponse.Write ("Error:"+Ex. Message);  the }  the finally  + {  - if(FileStream! =NULL)  the { Bayi //Close File the filestream.close (); the }  - Response.End (); - }  the }  the}
View Code

Webform (File upload)

Related Article

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.