ASP. NET implements resumable upload of Files

Source: Internet
Author: User

When downloading an object, you can use resumable upload to continue downloading the object that was not downloaded last time. This feature is very important when downloading the object. Here I will introduce a simple method to implement the resumable upload function, which is only for beginners. You can skip this step...
Here, I add an HTTP header to the output stream through the AddHeader method in the Response class. The HTTP header consists of header information and body information. The two are separated by a blank line. Here, the Range segment is added to the header to indicate where the client wants to continue downloading to implement the Resume function.
Let's get started.
1. Create a new home page with a name.
2. Add a LinkButton to the page to execute the implementation process.
3. Implement the resumable upload function in the Click Event of the LinkButton.
The Code is as follows:
In addition, do not forget to reference the System. IO namespace. Here we only paste the background implementation code (the front-end will not be able to go back to learning ...)

Using System; using System. data; using System. configuration; using System. collections; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. IO; public partial class DFile: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {} protected void LinBtnDFile _ Click (object sender, EventArgs e) {// create a bit array byte [] buffer = new Byte [10240]; // specify the path of the file to be downloaded. string filePath = @ "D: \ .rar"; // or the file name contains the extension string fileName = Path. getFileName (filePath); Stream fileStream = null; try {// open the file fileStream = new FileStream (filePath, FileMode. open, FileAccess. read, FileShare. read); Response. clear (); // obtain the file size long fileSize = fileStream. length; long sum = 0; if (Re Quest. Headers ["Range"]! = Null) {Response. StatusCode = 206; // an integer that indicates the HTTP output status returned to the client. The default value is 200. Sum = long. parse (Request. headers ["Range"]. replace ("bytes = ",""). replace ("-", "");} if (sum! = 0) {Response. addHeader ("Content-Range", "bytes" + sum. toString () + "-" + (long) (fileSize )). toString () + "/" + fileSize. toString ();} // get some http header information Response. addHeader ("Content-Length", (long) (fileSize-sum )). toString (); Response. contentType = "application/octet-stream"; // obtain the file source Response. addHeader ("Content-Disposition", "attachment; filename =" + HttpUtility. urlEncode (Request. contentEncodin G. getBytes (fileName); // Response. flush (); fileStream. position = sum; // set the current stream Position fileSize = fileSize-sum; // when the file size is greater than 0, it enters the loop while (fileSize> 0) {// determine whether the client is still connected to the server if (Response. isClientConnected) {// obtain the total number of bytes in the buffer. int length = fileStream. read (buffer, 0, 10240); // write data Response. outputStream. write (buffer, 0, length); // sends the buffer output to the client Response. flush (); buffer = new Byte [10240]; fileSize = fileSiz E-length;} else {// exit loop fileSize =-1 ;}} catch (Exception ex) {Response. write ("Error:" + ex. message);} finally {if (fileStream! = Null) {// Close the file fileStream. Close () ;}response. End ();}}}

It is relatively simple here, please make appropriate changes according to the actual situation.

 

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.