[Translation] File Upload progress bar in ASP. NET (C #)

Source: Internet
Author: User

Author:Azamsharp
Translation: sharpcn

Introduction:
A few days ago, I saw a question on the Forum about how to create a file upload progress bar. As you know, we have a control for uploading client files to the server, but this control does not provide any information about the upload progress. If the client uploads a file (especially a large file) without any changes on the page, it is difficult to know whether the upload operation is ongoing and how many files have not been uploaded yet. The solution is to use some technical methods to display the upload progress on the client.
Background:
In this article, I will not explain how to upload files to the server ". I would like to recommend Konstantin vasserman's wonderful article: "uploading files with ASP. NET ".
I want to talk about Server Upload. If the client does not submit resource requests to the server, the server will not send any information to the client, so it is impossible to send a progress message for file upload to the client. To solve this problem, the client must send a request to the server. The following JavaScript code will relax the request to the server two seconds later.
<SCRIPT>
SetTimeout ("_ dopostback ('btnrefresh ','') ", 2000 );
</SCRIPT>
In this Code, I used the [PostBack] event that can indicate the upload information. She will trigger the [_ dopostback] event created by Asp.net.
How to do this:
Well, the truth is very simple. The client requests an upload page from the server. When she loads the page for the first time, it displays the upload form page. When the "Upload" button is clicked, The PostBack event is sent to the server. The server code creates a process for uploading files and displaying the final information. The page uses the above JavaScript code to refresh the page every 2 seconds and update the upload information. After the file is uploaded, the result is displayed.

The key point is to read the data in the data packet. The following is the Program for uploading files.
Private void uploadthread ()
{
Byte [] B = new byte [1024];
Int READ = 0;
Try
{
Using (filestream FS = new filestream (_ filename, filemode. Create ))
{
While (read = _ stream. Read (B, 0, B. Length)> 0)
{
FS. Write (B, 0, read );
_ Bytesread + = read );
System. Threading. thread. Sleep (100 );
}
}
}
Catch (exception ex)
{
_ Exception = ex;
}
_ Uploaded = true;
}

As you can see, the data is loaded in 1024 bytes. The bytesread attribute is updated each time a data packet is copied. The more buffering, the larger the difference (this effect is obvious when uploading large files sharpcn note .)
Example:
This fileuploadtread class already supports data upload. All you need to do now is instantiate this class.
Fileuploadthread fut = new fileuploadthread (server. mappath ("files/the_filename"), thefile. postedfile. inputstream );
Continue to obtain information about the upload process.
If (! Fut. Upload)
{
// Read Process Information
String info = string. Format ("{0} of {1} bytes {2} %)", fut. bytesread, fut. length, fut. persent );
}
Else
{
// The file has been uploaded.
}

To exit upload, you only need to call the cancel () method of the fileuploadthread class.
Fut. Cancel ();

Here is a tricky thing. If the user closes the browser, the upload will continue until the upload times out or is complete. So far, I have not found a solution to this bug.

This example describes all the necessary information required for the upload page.

  • Upload form
  • Upload progress bar
  • Upload result

Another thing I want to say is that ASP. NET limits the size of uploaded data. If the data exceeds the maximum value (maxrequestleght), the request will not be processed. The maximum value is determined by the httpruntime of Web. config or machine. config. Here is an example of how to set the maximum value to 10 MB.
<Httpruntime maxrequestlength = "10240"
Usefullyqualifiedredirecturl = "true"
Executiontimeout = "45"/>

For more information, see msdn.

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.