Spark-md5 git链接: https://github.com/satazor/js-spark-md5
大文件切片计算md5值
self.chunkSize = 1024 * 1024 * 10 // 每个切片大小为10M
import SparkMD5 from 'spark-md5' //
self.getFileMd5 = file => {
return new Promise((resolve, reject) => {
const blobSlice =
File.prototype.slice ||
File.prototype.mozSlice ||
File.prototype.webkitSlice
const chunks = Math.ceil(file.size / self.chunkSize)
let currentChunk = 0
const spark = new SparkMD5.ArrayBuffer()
const fileReader = new FileReader()
fileReader.onload = function (e) {
spark.append(e.target.result)
currentChunk++
if (currentChunk < chunks) {
loadNext()
} else {
const _md5 = spark.end()
resolve(_md5)
}
}
function loadNext () {
const start = currentChunk * self.chunkSize
const end = start + self.chunkSize
fileReader.readAsArrayBuffer(blobSlice.call(file, start, end))
}
loadNext()
})
}
Spark-md5 部分API记录
1. SparkMD5.append(str):Appends a string, encoding it to UTF8 if necessary. // 有道翻译: 附加一个字符串,必要时将其编码为UTF8。
2. SparkMD5.appendBinary(str):Appends a binary string (e.g.: string returned from the deprecated readAsBinaryString). // 追加一个二进制字符串(例如:从已弃用的readAsBinaryString返回的字符串)。
3. SparkMD5.end(raw):Finishes the computation of the md5, returning the hex result. If raw is true, the result as a binary string will be returned instead. // 完成md5的计算,返回十六进制结果。如果raw为true,则返回二进制字符串形式的结果
4. SparkMD5.hash(str, raw):Hashes a string directly, returning the hex result. If raw is true, the result as a binary string will be returned instead. Note that this function is static. // 直接哈希字符串,返回十六进制结果。如果raw为true,则返回二进制字符串形式的结果。注意,这个函数是静态的。
5. SparkMD5.ArrayBuffer.append(arr):Appends an array buffer. // 追加一个数组缓冲区。
6. SparkMD5.ArrayBuffer。end(raw):Finishes the computation of the md5, returning the hex result. If raw is true, the result as a binary string will be returned instead. // 完成md5的计算,返回十六进制结果。如果raw为true,则返回二进制字符串形式的结果
7. SparkMD5.ArrayBuffer.hash(arr, raw):Hashes an array buffer directly, returning the hex result. If raw is true, the result as a binary string will be returned instead. Note that this function is static. // 直接哈希数组缓冲区,返回十六进制结果。如果raw为true,则返回二进制字符串形式的结果。注意,这个函数是静态的