UNPKG

yyzone

Version:

yyzone vue components and utils

35 lines (26 loc) 1.01 kB
import SparkMD5 from 'spark-md5'; const chunkSize = 1048576; export function calcFileMd5(file) { return new Promise((resolve, reject) => { const blobSlice = File.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice; const spark = new SparkMD5(); const fileReader = new FileReader(); const chunkTotal = Math.ceil(file.size / chunkSize); let currentChunk = 0; fileReader.onload = e => { spark.append(e.target.result); currentChunk ++; if (currentChunk < chunkTotal) readNext(); else { resolve(spark.end()) } } fileReader.onerror = err => reject(err); function readNext() { const start = currentChunk * chunkSize; const end = ((start + chunkSize) >= file.size) ? file.size : start + chunkSize; fileReader.readAsArrayBuffer(blobSlice.call(file, start, end)) } readNext(); }) }