下载后端返回的base64格式的图片
downloadLogo(id) {
let json = {
id: id,
};
this.download(json).then((res) => {
console.log(res, "======sss=======");
// 这里是获取到的图片base64编码res.file,这里只是个例子哈,要自行编码图片替换这里才能测试看到效果
const imgUrl = `data:image/jpg;base64,${res.file}`;
// 如果浏览器支持msSaveOrOpenBlob方法(也就是使用IE浏览器的时候),那么调用该方法去下载图片
if (window.navigator.msSaveOrOpenBlob) {
const bstr = atob(imgUrl.split(",")[1]);
let n = bstr.length;
const u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
const blob = new Blob([u8arr]);
window.navigator.msSaveOrOpenBlob(blob, "logo图片.jpg");
} else {
// 这里就按照chrome等新版浏览器来处理
const a = document.createElement("a");
a.href = imgUrl;
a.setAttribute("download", "logo图片.jpg");
a.click();
}
});
},
原文参考:https://blog.csdn.net/xiaozhidian/article/details/129166541?spm=1001.2101.3001.6650.5&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-5-129166541-blog-95431330.235%5Ev38%5Epc_relevant_anti_vip_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-5-129166541-blog-95431330.235%5Ev38%5Epc_relevant_anti_vip_base&utm_relevant_index=10