By utilizing the File APIs, we have the ability to simplify the process of uploading large files. The strategy involves breaking the upload into smaller chunks, initiating an XHR for each segment, and assembling the file on the server side. This method mirrors how GMail handles large attachments with speed and efficiency. It could also serve as a workaround for Google App Engine's 32MB HTTP request limitation.
function upload(blobOrFile) {
var xhr = new XMLHttpRequest();
xhr.open('POST', '/server', true);
xhr.onload = function(e) { ... };
xhr.send(blobOrFile);
}
document.querySelector('input[type="file"]').addEventListener('change', function(e) {
var blob = this.files[0];
const BYTES_PER_CHUNK = 1024 * 1024; // Chunk size set to 1MB.
const SIZE = blob.size;
var start = 0;
var end = BYTES_PER_CHUNK;
while(start < SIZE) {
upload(blob.slice(start, end));
start = end;
end = start + BYTES_PER_CHUNK;
}
}, false);
})();
The code snippet does not include instructions on reassembling the file on the server side.
Furthermore, functions like http://underscorejs.org/#zip can also be leveraged in this context.