The download function of Internet Explorer does not have the breakpoint continuation function. To achieve breakpoint continuation function, you need to use several little-known response headers and request headers in THE HTTP protocol.

One. Two necessary response headers accept-ranges, ETag

Each time a client submits a download request, the server adds these two response headers to ensure that the client and server recognize the download as a resumable download:

Accept-ranges: inform the download client that this is a download that can be resumed, store the starting byte position of this download and the byte size of the file;

ETag: a unique identifier to save the file (I am using the filename + the last modification time of the file to verify the file when the request is renewed);

Last-modified: An optional response header that stores the Last modification time of the server file for verification

An important request header Range

Range: For the first download, the Range header is null. In this case, response headers accept-ranges and ETag must be added to the server response header.

When the request is renewed, the value represents the number of bytes received by the client, that is, the start byte position of the download. The server reads data from the corresponding position according to this value and sends it to the client.

3. Request header if-range for validation

When the response header contains Accept-ranges and ETag, these request headers will be included when the request is forwarded:

If-range: indicates the ETag value of the response header.

Unless- modified-since: the last-modified value corresponding to the response header.

Continuingly request, in order to ensure the consistency and the correctness of the client and server files, it is necessary for file verification, validation need to write their own validation code, according to parse the two request header value, the client has been compared with server-side file download part, if not consistent, is to start from scratch to download, if match, the breakpoint continuingly.

Speed limit

The program added a speed limit, for the client access control of the flow limit.

Five. Other matters needing attention

Such as: filename garbled problem, filename space change plus sign, forced client display download dialog box, see source notes:

  1

2 / / /

3 /// Download files, support large files, continuation, speed limit. Support continuous response headers accept-ranges, ETag, request header Range.

4 /// Accept-ranges: response header that indicates to the client that this process supports resumable downloads. Implement background intelligent transmission service (BITS). The value is bytes.

5 /// ETag: response header, used for the initial (200) response to the client, and the recovery request from the client,

6 /// Each file must be provided with a unique ETag value (which can be made up of the filename and the date the file was last modified), which enables client software to verify that the byte blocks they have downloaded are still up to date.

7 /// Range: indicates the start position of the extension, that is, the number of bytes downloaded to the client. Bytes =1474560-.

8 /// In addition: UrlEncode will convert the space in the file name to + (+ to %2b) after encoding, but the browser cannot understand the plus sign as a space, so the downloaded file in the browser, the space will become a plus sign;

9 /// Workaround: after UrlEncode, replace “+” with “%20” because the browser converts %20 to a space

10 / / /

11 /// The HttpContext of the current request

12 /// Physical path of the downloaded file, including path and file name

13 /// Download speed: the number of bytes that can be downloaded per second

14 /// True Indicates that the download succeeds. False indicates that the download fails

 15        public static bool DownloadFile(HttpContext httpContext, string filePath, long speed)

 16        {

 17            bool ret = true;

 18            try

 19            {

20 — Verify: HttpMethod, whether the requested file exists

 36

37 Define local variables

 49

50 – validation: if the file is too large, is continuingly, and after the last requested date ever been modified — — — — — — — — — — — — — —

 67

 68                try

 69                {

70 ——- Add important response headers, parse request headers, and relevant validation ——————-

 97

98 ——- Send data blocks to the client ——————-

108}

109                catch

110                {

111                    ret = false;

112}

113                finally

114                {

115                    br.Close();

116                    myFile.Close();

117}

118}

119            catch

120            {

121                ret = false;

122}

123            return ret;

124}

Upload and display screenshots:

Detailed configuration information can be reference for me to write this article: blog.ncmem.com/wordpress/2…

Welcome to join the group to discuss “374992201”