//base64 转换 blob
dataURLtoBlob(dataurl) {
var bstr = atob(dataurl)
var n = bstr.length;
var u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: ‘pdf‘ });
}
// res.data 就是后台返回的base64的 文件流
let URL = this.dataURLtoBlob(res.data);
var reader = new FileReader();
reader.readAsDataURL(URL);
reader.onload = function (e) {
// 兼容IE
if (window.navigator.msSaveOrOpenBlob) {
var bstr = atob(e.target.result.split(",")[1]);
var n = bstr.length;
var u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
var blob = new Blob([u8arr]);
window.navigator.msSaveOrOpenBlob(blob,‘采购单.pdf‘);
} else {
// 转换完成,创建一个a标签用于下载
var a = document.createElement(‘a‘);
a.download = ‘采购单.pdf‘;
a.href = e.target.result;
a.setAttribute("id", "export")
document.getElementById("purchaseManage").append(a); // 修复firefox中无法触发click
a.click();
document.getElementById("export").remove();
}
}
原文地址:https://www.cnblogs.com/aoqizhonghua/p/11093087.html