/**
 * File to Blob
 * @param { File } 文件
 */
const fileToBlob = (file = new File()) => new Promise((resolve, reject) => {
    let chunkSize = 1024 * 1024; // 1MB
    let total = Math.ceil(file.size / chunkSize);
    let index = 0;
    let chunks = [];

    let next = () => {
        let start = index * chunkSize;
        let end = Math.min(start + chunkSize, file.size);
        let blob = file.slice(start, end);
        let reader = new FileReader();
        reader.onload = () => {
            chunks.push(reader.result);
            index++;
            if (index < total) next();
            else resolve(new Blob(chunks, { type: file.type }));
        };
        reader.onerror = error => reject(error);
        reader.readAsArrayBuffer(blob);
    };

    next();
});

文章作者: CaptainTwo
本文链接:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 CaptainTwo
JavaScript JavaScript
喜欢就支持一下吧