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 mime = arr[0].match(/:(.*?);/)[1];
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";
// 字符内容转变成blob地址
var blob = new Blob([this.dataURLtoBlob(res.data.data)]);
eleLink.href = URL.createObjectURL(blob);
// 触发点击
document.body.appendChild(eleLink);
eleLink.click();
// 然后移除
document.body.removeChild(eleLink);