采用 FileReader
对文件进行分段读取,通过 CryptoJS
对文件进行分段加密,需要引用 import CryptoJS from 'crypto-js';
function calculateMD5InChunks(file, chunkSize) {
return new Promise((resolve) => {
const md5 = CryptoJS.algo.MD5.create();
const reader = new FileReader();
let currentChunk = 0;
reader.onload = function (e) {
// Update hash for this chunk
md5.update(CryptoJS.lib.WordArray.create(e.target.result));
currentChunk++;
if (currentChunk < Math.ceil(file.size / chunkSize)) {
// Read next chunk
reader.readAsArrayBuffer(file.slice(currentChunk * chunkSize, (currentChunk + 1) * chunkSize));
} else {
// All chunks read and processed, get the final hash
const hash = md5.finalize().toString(CryptoJS.enc.Hex);
resolve(hash);
}
};
// Start reading the file in chunks
reader.readAsArrayBuffer(file.slice(0, chunkSize));
})
}
async function uploadBtn(options) {
calculateMD5InChunks(options.file, 1024 * 64).then(async (rs) => {
const formData = new FormData()
formData.append('file', options.file)
formData.append('Md5', rs)
const data = await coreUploadApi2(formData);
console.log('data => ', data)
});
}