export function slice(file, chunkSize, progress) {
let blobSlice =
File.prototype.slice ||
File.prototype.mozSlice ||
File.prototype.webkitSlice;
let totalChunk = Math.ceil(file.size / chunkSize);
let currentChunk = 0;
let fileReader = new FileReader();
let chunks = [];
fileReader.onload = (e) => {
chunks.push(e.target.result);
currentChunk++;
if (progress) {
let progressValue = currentChunk / totalChunk;
let progressPercentValue = parseFloat((progressValue * 100).toFixed(2));
progress(progressPercentValue);
}
if (currentChunk < totalChunk) {
loadNext();
} else {
return Promise.resolve(chunks);
}
};
fileReader.onerror = () => {
return Promise.reject();
};
function loadNext() {
let beginIndex = currentChunk * chunkSize;
let endIndex = beginIndex + chunkSize;
if (endIndex >= file.size) {
endIndex = file.size;
}
fileReader.readAsArrayBuffer(blobSlice.call(file, beginIndex, endIndex));
}
loadNext();
}