Created
November 26, 2013 00:09
-
-
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.
This file contains 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
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