Asp.net file upload with progress bar implementation case (multiple styles), asp.net File Upload

Source: Internet
Author: User

Asp.net file upload with progress bar implementation case (multiple styles), asp.net File Upload

Full eye:

In the previous articles, there were similar cases of transferring files with progress bars. You can read the previous articles to expand the knowledge points.

Some code:

<% @ Page Language = "C #" %> <% @ Register Assembly = "MattBerseth. webControls. AJAX "Namespace =" 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 "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> Untitled Page </title> <link rel =" Stylesheet "href =" _ assets/css/progress.css "mce_href = "_ assets/css/progress.css "/> <link rel =" Stylesheet "href =" _ assets/css/upload.css "mce_href =" _ assets/css/upload.css "/> <mce: style type = "text/css"> <! -- BODY {font-family: Arial, Sans-Serif; font-size: 12px;} --> </mce: style> <style type = "text/css" mce_bogus = "1"> BODY {font-family: Arial, Sans-Serif; font-size: 12px ;} </style> <mce: script type = "text/C #" runat = "server"> <! -- Protected void Page_Load (object sender, EventArgs args) {if (! This. isPostBack) {this. session ["UploadInfo"] = new UploadInfo {IsReady = false };}/// <summary> /// </summary> [System. web. services. webMethod] [System. web. script. services. scriptMethod] public static object GetUploadStatus () {// obtain the object length UploadInfo info = HttpContext. current. session ["UploadInfo"] as UploadInfo; if (info! = Null & info. isReady) {int soFar = info. uploadedLength; int total = info. contentLength; int percentComplete = (int) Math. ceiling (double) soFar/(double) total * 100); string message = string. format ("Upload {0 }... {1} of {2} bytes ", info. fileName, soFar, total); // return percentage return new {percentComplete = percentComplete, message = message};} // not ready yet... return null;} // --> </mce: script> 

 Upload. aspx:

// Size limit 1 M 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 that the upload const string js = "window. parent. onComplete ('error', 'file cannot be uploaded. Please refresh the page and try again); "; ScriptManager. registerStartupScript (this, typeof (upload_aspx), "progress", js, true);} else {// Let the server know that we are not ready yet .. uploadInfo. isReady = false; // upload verification if (this. fileUpload. postedFile! = Null & this. fileUpload. postedFile. contentLength> 0 & this. fileUpload. postedFile. contentLength <1048576) // limit 1 M {// set the 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 initialization exists... UploadInfo. isReady = true; // cache int bufferSize = 1; byte [] buffer = new byte [bufferSize]; // Save the byte using (FileStream fs = new FileStream (Path. combine (path, fileName), FileMode. create) {while (uploadInfo. uploadedLength <uploadInfo. contentLength) {// buffer int bytes = this from the input stream. fileUpload. postedFile. inputStream. read (buffer, 0, bufferSize); // bytes are written to the file stream fs. write (buffer, 0, bytes); // update uploadInfo. U PloadedLength + = bytes; // The thread will be slower during sleep upload so that the progress bar is displayed. threading. thread. sleep (100) ;}// Delete. file. delete (Path. combine (path, fileName); // Let the parent page know that the uploaded data has been processed. const string js = "window. parent. onComplete ('success', '{0} uploaded successfully'); "; ScriptManager. registerStartupScript (this, typeof (upload_aspx), "progress", string. format (js, fileName), true);} else {if (this. fileUpload. postedFile. contentLength >=104 8576) // 1 M {const string js = "window. parent. onComplete ('error', 'exceeds the size limit of the uploaded file, please reselect '); "; 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 ;}}// no size limit protected void Pag E_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} uploaded successfully'); "; 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 will not be pasted up, just do the goods directly, kiss, this is free of mail!

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.