base64 转 file / blob
const base64 = '';
let arr = base64.split(','),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while(n--) {
u8arr[n] = bstr.charCodeAt(n);
}
const file = new File([u8arr], 'file.name', {type: mime}); // file
const blob = new Blob([u8arr], {type: mime}); // blob
file / blob 转 base64
function change(file) {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = e => {
resolve(e.target.result);
}
reader.onerror = error => {
resolve(error);
}
reader.readAsDataURL(file);
})
}
const base64 = change(file);
blob 转 file
function change(blob, fileName) {
blob.lastModifiedDate = new Date();
blob.name = fileName;
return blob;
}
const file = change(blob, fileName);