base64转Blob
dataURLtoBlob = (dataurl) => {
try {
let arr = dataurl.split(',');
let mime = arr[0].match(/:(.*?);/)[1];
let bstr = atob(arr[1]);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
} catch{
let arr = dataurl.split(',');
let bstr = atob(arr[0]);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr]);
}
}
Blob转文件下载
res.data.fileName:后端带来的文件名; res.data.data base64串;
var eleLink = document.createElement("a");
eleLink.download = res.data.fileName;
eleLink.style.display = "none";
var blob = new Blob([this.dataURLtoBlob(res.data.data)]);
eleLink.href = URL.createObjectURL(blob);
document.body.appendChild(eleLink);
eleLink.click();
document.body.removeChild(eleLink);
|