Principle and realization method of asp.net interruption point sharing _ practical skills

Source: Internet
Author: User
Tags ranges
A request protocol is a protocol that sends a message when a client (browser) submits a request to the server (WEB server). A reply protocol is a protocol that the server (Web server) responds to a message to a client (browser). Both the request and reply protocols are composed of a header and a body. The head and body are separated by a line of empty behavior.
The following is an example of a request message and a corresponding reply message:
Copy Code code 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 OK
server:microsoft-iis/5.0
Date:tue, June 2003 05:39:40 GMT
Content-type:image/jpeg
Accept-ranges:bytes
Last-modified:thu, May 2002 03:05:40 GMT
ETag: "bec48eb862c21:934"
content-length:2827
....

As the name suggests, the continuation of a breakpoint is the last time the download was disconnected at the beginning of the download. In the HTTP protocol, you can include a range segment in the request message header to indicate where the client wants to continue downloading.
For example, starting from the 1024th byte download, the request message is as follows:
Copy Code code 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
range:bytes=1024-
Connection:keep-alive

The corresponding response message is
Copy Code code as follows:

http/1.1 206 Partial Content
server:microsoft-iis/5.0
Date:tue, June 2003 05:39:40 GMT
Content-type:image/jpeg
Accept-ranges:bytes
Last-modified:thu, May 2002 03:05:40 GMT
ETag: "bec48eb862c21:934"
content-length:1803
Content-range:bytes 1024-1803/2827

Through two different messages can be seen, in the extension of the breakpoint, as long as we can give the corresponding message to the client, so that the client can respond correctly, and the transmission point after the transfer of some of the files can be implemented to achieve a breakpoint extension.
1. Distinguish the continuous transmission message of the breakpoint.
Because the break-through message contains a Range field, it is only null by request.headers["range".
2. Send the correct resume response message
Two response messages in different parts of the message has been identified with the red part, just modify the red part of the message header, you can send the correct continuation message.
3. Transfer the correct part of the file
The continuation of the transmission point only need to transfer after the file can be, first through the request message in the Range field to get the beginning of the location of the file, the transfer of the file only need to transfer the part after that location.
The following code example shows a ASP.net page that can support the continuation of a breakpoint
Copy Code code 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 file transferred
Long Filetranlen = fi. Length;

Breakpoint Renewal 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]-[file block total size of transfer file] [total size of file]
Response.AddHeader ("Content-range", String. Format ("bytes {0}-{1}/{2}", Startpos,filetranlen,fi.) Length));
}

Response.AddHeader ("Content-length", filetranlen.tostring ());

Basic file Download Message headers
Response.ContentType = "Application/octet-stream";
Response.AddHeader ("Content-disposition", "attachment; Filename= "+ fi. Name);

A simple streaming 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.