Skip to content

Instantly share code, notes, and snippets.

@jarrodhroberson
Created November 26, 2013 00:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarrodhroberson/7651199 to your computer and use it in GitHub Desktop.
Save jarrodhroberson/7651199 to your computer and use it in GitHub Desktop.
Reading a File in chunks with the HTML5 FileReader API and calculating MD5 hash codes of the contents with the spark-md5 library in a WebWorker.
importScripts('spark-md5.min.js');
function calcMD5(f) {
var blobSlice = Blob.prototype.slice;
var chunkSize = 2097152;
var chunks = Math.ceil(f.size/chunkSize);
var spark = new SparkMD5.ArrayBuffer();
var currentChunk = 0;
var fr = new FileReader();
fr.onload = function(e) {
spark.append(e.target.result);
if (currentChunk === chunks) {
postMessage({name:f.name, md5:spark.end()});
} else {
postMessage({name:f.name, md5:currentChunk+' / '+chunks});
var start = currentChunk * chunkSize;
var end = ((start + chunkSize) >= f.size) ? f.size : start + chunkSize;
fr.readAsArrayBuffer(blobSlice.call(f, start, end));
currentChunk++;
}
};
fr.onerror = function(e) {
postMessage({name:f.name, md5:e.message});
};
// kick off the reading of the file
fr.readAsArrayBuffer(blobSlice.call(f,currentChunk + chunkSize, chunkSize));
}
onmessage = function(e) {
e.data.forEach(function(f){
calcMD5(f);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment