function md5WithChunk(file, chunkSize, processCallback = console.log) { return new Promise((resolve, reject) => { const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice let chunks = Math.ceil(file.size / chunkSize) let currentChunk = 0 let spark = new SparkMD5.ArrayBuffer() let fileReader = new FileReader() fileReader.onerror = reject fileReader.onload = (e) => { processCallback(currentChunk / chunks) spark.append(e.target.result) currentChunk++ if (currentChunk < chunks) { loadNext() } else { resolve(spark.end()) } } function loadNext() { let start = currentChunk * chunkSize let end = start + chunkSize >= file.size ? file.size : start + chunkSize fileReader.readAsArrayBuffer(blobSlice.call(file, start, end)) } loadNext() }) }
使用