Code for resumable upload of ASP. NET files

Source: Internet
Author: User

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 ...) Copy codeThe Code is as follows: 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 object to be downloaded.
String filePath = @ "D: \ AI Zhiqiang .rar ";
// Or get the file name including 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 (Request. 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 ());
}
// Obtain 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. ContentEncoding. GetBytes (fileName )));
// Response. Flush ();
FileStream. Position = sum; // you can specify 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 );
// Send the buffer output to the client
Response. Flush ();
Buffer = new Byte [10240];
FileSize = fileSize-length;
}
Else
{
// Exit the loop after the user is disconnected
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.

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.