asp.net implementation file upload with progress bar (a variety of styles)

Source: Internet
Author: User

File upload with progress bar a variety of styles are very beautiful.

Friendly hints as well as upload verification.

Part of the code:

<%@ Page language= "C #"%> <%@ Register assembly= "MattBerseth.WebControls.AJAX" MattBerseth.WebControls.AJAX.Progress "tagprefix=" MB%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

Upload.aspx:

Limit size 1M protected void Page_load2 (object sender, EventArgs e) {if (this. IsPostBack) {Uploadinfo Uploadinfo = this. session["Uploadinfo"] as Uploadinfo; if (Uploadinfo = = null) {//Let the parent page know cannot process upload const string JS = "Window.parent.onComplete (' error ', ' cannot upload file.) Please refresh the page and try again); Scriptmanager.registerstartupscript (This, typeof (Upload_aspx), "Progress", JS, True); else {//Let the service side know we're not ready yet. Uploadinfo.isready = false;//upload Verify if (this.fileUpload.PostedFile!= null && this.fi LeUpload.PostedFile.ContentLength > 0 && this.fileUpload.PostedFile.ContentLength < 1048576)//Limited 1M {// Set path string path = this. Server.MapPath (@ "uploads"); String fileName = Path.getfilename (this.fileUpload.PostedFile.FileName); Upload information uploadinfo.contentlength = this.fileUpload.PostedFile.ContentLength; Uploadinfo.filename = FileName; uploadinfo.uploadedlength = 0; File exists initialization ... uploadinfo.isready = true; cache int buffersize = 1; byte[] buffer = new Byte[buffersize]; Save byte using (FileStream FS= New FileStream (Path.Combine (Path, FileName), FileMode.Create)) {while (Uploadinfo.uploadedlength < Uploadinfo.contentlength) {//from input into buffer int bytes = This.fileUpload.PostedFile.InputStream.Read (buffer, 0, buffersize) ; Bytes are written to the file stream fs. Write (buffer, 0, bytes); Update size Uploadinfo.uploadedlength + = bytes; Thread sleep uploads are slower so you can see the progress bar System.Threading.Thread.Sleep (100); }//Delete. File.delete (Path.Combine (Path, fileName)); Let the parent page know has been processed upload const string js = "Window.parent.onComplete (' success ', ' {0} has been successfully uploaded ');"; Scriptmanager.registerstartupscript (This, typeof (Upload_aspx), "Progress", string. Format (JS, fileName), true); else {if (this.fileUpload.PostedFile.ContentLength >= 1048576)//1m {const string JS = "Window.parent.onComplete" (' ER Ror ', ' exceed upload file limit size, please select ' Again '; Scriptmanager.registerstartupscript (This, typeof (Upload_aspx), "Progress", JS, True); else {const string JS = ' window.parent.onComplete (' error ', ' Upload file error '); Scriptmanager.registerstartupscript (This, typeof (Upload_aspx), "Progress ", JS, True); } Uploadinfo.isready = false; }}//Do not limit size protected void Page_Load (object sender, EventArgs e) {if (this. IsPostBack) {Uploadinfo Uploadinfo = this. session["Uploadinfo"] as Uploadinfo; Uploadinfo.isready = false; if (this.fileUpload.PostedFile!= null && this.fileUpload.PostedFile.ContentLength > 0) {string path = this. Server.MapPath (@ "uploads"); String fileName = Path.getfilename (this.fileUpload.PostedFile.FileName); Uploadinfo.contentlength = This.fileUpload.PostedFile.ContentLength; Uploadinfo.filename = FileName; uploadinfo.uploadedlength = 0; Uploadinfo.isready = true; int buffersize = 1; byte[] buffer = new Byte[buffersize]; using (FileStream fs = new FileStream (Path.Combine (Path, FileName), FileMode.Create)) {while (uploadinfo.uploadedlength < uploadinfo.contentlength) {int bytes = this.fileUpload.PostedFile.InputStream.Read (buffer, 0, buffersize); fs. Write (buffer, 0, bytes); Uploadinfo.uploadedlength + = bytes; } const String JS = "Window.parent.onComplete (' success ', ' {0} has been successfully uploaded '); "; Scriptmanager.registerstartupscript (This, typeof (Upload_aspx), "Progress", string. Format (JS, fileName), true); else {const string JS = ' window.parent.onComplete (' error ', ' Upload file error '); Scriptmanager.registerstartupscript (This, typeof (Upload_aspx), "Progress", JS, True); } Uploadinfo.isready = false; } }

The code is not finished, just package and download it.

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.