js large file uploads

I. Overview

 

The so-called HTTP, in fact, refers only to download a file that is already downloaded from place to start to continue downloading. In the previous version of the HTTP protocol does not support breakpoints, HTTP / 1.1 beginning supported. Breakpoints generally used when downloading Range and Content-Range entity-header. HTTP protocol itself does not support the breakpoint upload, it needs its own implementation.

 

Two, Range 

 

The request header is used to specify the position of the first byte and the last byte of data, the general format:

 

    Range: a client requests the server can specify a certain period of the download file size followed by changing field, a byte offset from zero. Typical format:

    Ranges:    (unit=first byte pos)-[last byte pos]

    Ranges: bytes = 4000- 4000 bytes downloaded from the first portion to the end of file

    Ranges: bytes = 0 ~ N 0-N of the download byte range

    Ranges: bytes = MN MN downloads the first byte range

    Ranges: bytes = -N last N bytes downloaded content



 

1. The following points should be noted:

(1) This data section is closed interval, the start value is 0, the "Range: bytes = 0-1" in such a request is actually 2 bytes beginning of the request.

(2) "Range: bytes = -200", it is not used 201 bytes of the requested file start position, but rather a request for 200 bytes at the end of the file.

(3) If the last byte pos is less than the first byte pos, then the Range request is invalid requests, server need to ignore the Range request and respond with a 200, the entire file is sent to client.

(4) If the last byte pos than or equal to the length of the file, then the Range request can not be considered satisfied, server need to respond to a 416, Requested range not satisfiable.

 

2. Example explained:

Represents the first 500 bytes: bytes = 0-499  

Represents the second 500 bytes: bytes = 500-999  

Denotes the last 500 bytes: bytes = -500  

500 bytes beyond the range indicated: bytes = 500-  

The first and last bytes: bytes = 0-0, -1  

Specify several ranges: bytes = 500-600,601-999 

 

Three, Content-Range

 

Responsive head, designated insertion position in a portion of the entire body, he also indicates the length of the entire entity. Returns a response to the client part of the server, it must describe the overall physical length of the response from the scope and coverage. The general format: 

 

Content-Range: bytes (unit first byte pos) - [last byte pos]/[entity legth] 

 

Four, Header Example

 

Request to download the entire file: 

 

GET /test.rar HTTP/1.1 

Connection: close 

Host: 116.1.219.219 

Range: bytes = 0-801 // general request to download the entire file is bytes = 0- or without the head

 

The normal response 

 

HTTP/1.1 200 OK 

Content-Length: 801      

Content-Type: application/octet-stream 

Content-Range: bytes 0-800 / 801 // 801: total file size

 

One of the most simple HTTP achieve something like:

1. 1024K client to download a file, which has been downloaded 512K

2. Network interruption, resume the client request, and therefore need to affirm this segment needs to resume the HTTP header:

Range:bytes=512000-

512K location of this head start transferring files from a file server notification

3. The HTTP server receives the request, transmitted from 512K start location of the file, and the increase in the HTTP header:

Content-Range:bytes 512000-/1024000

And at this time the server returns an HTTP status code should be 206 instead of 200.

However, in the actual scene, there will be a situation in which when the terminal initiates the resume request, URL corresponding to the content of the document has changed on the server, then resume data it is certainly wrong. How to solve this problem? Obviously at this point we need to have a way to identify the uniqueness of the file. In RFC2616 also has the corresponding definition, such as the last modified time to achieve Last-Modified identified file has been altered and whether that would occur when it is determined that file resumes. At the same time there is also defined in RFC2616 header of a ETag, uniquely identifies the placement head may be used ETag file, such as MD5 value document.

Terminal should affirm If-Match or If-Modified-Since HTTP header field when initiating resume request, to help determine the server file changes.

Further in RFC2616 defines both a If-Range header, if the terminal is using the resume If-Range. The contents of the If-Range may initially receive the head or ETag Last-Modfied last modification time. Upon receipt of the server request resume, be verified by the content of the If-Range, consistency checking 206 returns response when resuming, inconsistent response server 200 returns, as a response to the content of all of the new file data.


Related reference links: http://blog.ncmem.com/wordpress/2019/08/09/http%e6%96%ad%e7%82%b9%e7%bb%ad%e4%bc%a0/ 
welcome into group to discuss: 374 992 201

Guess you like

Origin www.cnblogs.com/songsu/p/12587704.html