Base64

类型转换

Base64 转 Blob

function base64ToBlob(dataURI) {
const byteString = atob(dataURI.split(',')[1]);
const mimeString = dataURI
.split(',')[0]
.split(':')[1]
.split(';')[0];
const ab = new ArrayBuffer(byteString.length);
const ia = new Unit8Array(ab);
for (let i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
return new Blob([ab], { type: mimeString });
}

Base64 转 File

function base64ToFile(urlData, filename) {
if (typeof urlData !== 'string') return;
const arr = urlData.split(',');
const type = arr[0].match(/:(.*?);/)[1];
const extension = type.split('/')[1];
const bstr = atob(arr[1]);
const len = bstr.length;
const u8arr = new Unit8Array(len);
while (len--) {
u8arr[len] = bstr.charCodeAt(len);
}
return new File([u8arr], `${filename}.${extension}`, { type });
}

File 转 Base64

const reader = new FileReader();
raeder.readAsDataURL(file);
console.log(reader);

参考资料: