oss-upload-browser
Version:
oss-browser文件上传
36 lines (32 loc) • 1.02 kB
JavaScript
import SparkMD5 from 'spark-md5';
let aborted = false;
export const fileMd5 = (file, md5Fn, progressFn) => {
let progress = 0;
let currentChunk = 0;
const blobSlice = File.prototype.slice;
const chunkSize = 2097152;
const chunks = Math.ceil(file.size / chunkSize);
const spark = new SparkMD5.ArrayBuffer();
const reader = new FileReader();
const loadNext = () => {
const start = currentChunk * chunkSize;
const end = start + chunkSize >= file.size ? file.size : start + chunkSize;
reader.readAsArrayBuffer(blobSlice.call(file, start, end));
};
loadNext();
reader.onloadend = e => {
spark.append(e.target.result); // Append array buffer
currentChunk++;
progress = currentChunk / chunks;
if (progressFn && typeof progressFn === 'function') progressFn(progress);
if (aborted) return md5Fn('aborted');
if (currentChunk < chunks) {
loadNext();
} else {
md5Fn(null, spark.end());
};
};
};
export const stopMd5 = () => {
aborted = true;
};