Base64 to Blob
/**
* Base64 to Blob
* @param { string } base64 Base64
*/
const base64ToBlob = (base64 = "") => new Promise((resolve, reject) => {
try {
let body = base64.split(",")[1];
let chunkSize = 1024;
let offset = 0;
let chunks = [];
while (offset < body.length) {
let chunk = body.slice(offset, offset + chunkSize);
let buffer = Uint8Array.from(atob(chunk), c => c.charCodeAt(0)).buffer;
chunks.push(buffer);
offset += chunkSize;
}
let buffers = new Uint8Array(chunks.reduce((acc, chunk) => acc + chunk.byteLength, 0));
let index = 0;
chunks.forEach(chunk => {
buffers.set(new Uint8Array(chunk), index);
index += chunk.byteLength;
});
resolve(new Blob([buffers], { type: base64.split(",")[0].replace(/data:|;base64/gi, "") }));
} catch (error) {
reject(error);
}
});
本文链接:
/archives/base64-to-blob
版权声明:
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
CaptainTwo!
喜欢就支持一下吧