Principles and implementation of resumable upload in Asp.net

Source: Internet
Author: User
Tags microsoft iis

A request protocol is a protocol used by a client (browser) to send a message when a request is submitted to the SERVER (web server. The reply protocol is the protocol used by the server to reply packets to the client (browser. Both request and response protocols are composed of headers and bodies. The header and body are separated by a blank line.
The following is an example of a request message and the corresponding response message: Copy codeThe Code is as follows: GET/image/index_r4_c1.jpg HTTP/1.1
Accept :*/*
Referer: http: // 192.168.3.120: 8080
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. net clr 1.0.3705)
Host: 192.168.3.120: 8080
Connection: Keep-Alive

HTTP/1.1 200 OK
Server: Microsoft-Microsoft IIS/5.0
Date: Tue, 24 Jun 2003 05:39:40 GMT
Content-Type: image/jpeg
Accept-Ranges: bytes
Last-Modified: Thu, 23 May 2002 03:05:40 GMT
ETag: "bec48eb862c21: 934"
Content-Length: 2827
....

As the name suggests, resumable download starts when the previous download is disconnected. In the HTTP protocol, you can add a Range segment to the request header to indicate where the client wants to continue downloading.
For example, when downloading from 1,024th bytes, the request message is as follows:Copy codeThe Code is as follows:

GET/image/index_r4_c1.jpg http/ 1.1
Accept :*/*
Referer: http: // 192.168.3.120: 8080
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. net clr 1.0.3705)
Host: 192.168.3.120: 8080
Value Range: bytes = 1024-
Connection: Keep-Alive

The corresponding response packet isCopy codeThe Code is as follows: HTTP/1.1 206 Partial Content
Server: Microsoft-Microsoft IIS/5.0
Date: Tue, 24 Jun 2003 05:39:40 GMT
Content-Type: image/jpeg
Accept-Ranges: bytes
Last-Modified: Thu, 23 May 2002 03:05:40 GMT
ETag: "bec48eb862c21: 934"
Content-Length: 1803
Content-Range: bytes 1024-1803/2827

We can see from two different packets that, during resumable data transfer, we only need to send corresponding packets to the client so that the client can respond correctly, in addition, you can transfer part of the files after the resumable upload point to achieve resumable upload.
1. differentiate resumable packets.
Because the resumable message contains the Range field, you only need to check whether Request. Headers ["Range"] is null.
2. Send the correct resume Response Message
Different parts of the two response packets are marked in red in the message. You only need to modify the red part of the packet header to send the correct resumed packet.
3. Transfer the correct part of the file
You only need to transmit the file after the resume point during the resume. First, you can obtain the start position of the file through the Range field in the request message, when transferring a file, you only need to transfer the part after this location.
The following code example shows an ASP. NET page that supports resumable upload.Copy codeThe Code is as follows: private void Page_Load (object sender, System. EventArgs e)
{
String file = MapPath ("ff.zip ");
FileInfo fi = new FileInfo (file );

Long startPos = 0;

// The length of the transmitted File
Long fileTranLen = fi. Length;

// Resumable upload request
If (Request. Headers ["Range"]! = Null)
{
Response. StatusCode = 206;
StartPos = long. Parse (Request. Headers ["Range"]. Replace ("bytes =", ""). Split ('-') [0]);
FileTranLen-= startPos;

// Response. AddHeader ("Accept-Ranges", "bytes ");
// Content-Range: bytes [start byte of the file block]-[total size of the file to be transferred]/[total size of the file]
Response. AddHeader ("Content-Range", string. Format ("bytes {0}-{1}/{2}", startPos, fileTranLen, fi. Length ));
}

Response. AddHeader ("Content-Length", fileTranLen. ToString ());

// Basic File Download Header
Response. ContentType = "application/octet-stream ";
Response. AddHeader ("Content-Disposition", "attachment; filename =" + fi. Name );

// Simple stream copy
System. IO. Stream fileStream = System. IO. File. OpenRead (file );
FileStream. Position = startPos;

Byte [] buffer = new Byte [1024];
Int count;
While (count = fileStream. Read (buffer, 0, buffer. Length)> 0)
{
Response. OutputStream. Write (buffer, 0, count );
}
FileStream. Close ();

Response. End ();
}

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.