Created
April 23, 2014 05:30
-
-
Save cuing/11203670 to your computer and use it in GitHub Desktop.
High Performance Browser Networking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the file is split and uploaded in chunks via multiple XHR requests | |
/* | |
Slicing large file uploads is a good technique to provide more robust | |
API where connectivity is unstable or intermittent—e.g., if a chunk | |
fails due to dropped connection, the application can retry, or resume | |
the upload later instead of restarting the full transfer from the start. | |
*/ | |
var blob = []; | |
const BYTES_PER_CHUNK = 1024 * 1024; | |
const SIZE = blob.size; | |
var start = 0; | |
var end = BYTES_PER_CHUNK; | |
while(start < SIZE) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('POST', '/upload'); | |
xhr.onload = function() { ... }; | |
xhr.setRequestHeader('Content-Range', start+'-'+end+'/'+SIZE); | |
xhr.send(blob.slice(start, end)); | |
start = end; | |
end = start + BYTES_PER_CHUNK; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment