方法封装
import SparkMD5 from 'spark-md5'
export const computedFileMD5 = (file = undefined) => {
return new Promise((resolve, reject) => {
if (!file) {
reject(undefined)
}
const blobSlice =
File.prototype.slice ||
File.prototype.mozSlice ||
File.prototype.webkitSlice
const chunkSize = 2097152 * 50
const chunks = Math.ceil(file.size / chunkSize)
let currentChunk = 0
const spark = new SparkMD5.ArrayBuffer()
let fileReader = new FileReader()
fileReader.onload = function (e) {
spark.append(e.target.result)
currentChunk++
if (currentChunk < chunks) {
loadNext()
} else {
const md5 = `${spark.end()}`
spark.reset()
spark.destroy()
fileReader = null
resolve(md5)
}
}
fileReader.onerror = function () {
reject(undefined)
}
function loadNext() {
const start = currentChunk * chunkSize
const end = start + chunkSize >= file.size ? file.size : start + chunkSize
fileReader.readAsArrayBuffer(blobSlice.call(file, start, end))
}
loadNext()
})
}
spark-md5