getBase64(imgUrl, iconMsg) {
window.URL = window.URL || window.webkitURL;
const xhr = new XMLHttpRequest();
xhr.open('get', imgUrl, true);
// 至关重要
xhr.responseType = 'blob';
xhr.onload = function () {
if (this.status === 200) {
// 得到一个blob对象
const blob = this.response;
// 至关重要
const oFileReader = new FileReader();
oFileReader.onloadend = function (e) {
// 此处拿到的已经是 base64的图片了
const base64 = e.target.result;
};
oFileReader.readAsDataURL(blob);
const src = window.URL.createObjectURL(blob);
const alink = document.createElement('a');
alink.href = src;
// 模拟鼠标click点击事件
const event = new MouseEvent('click');
// 设置a节点的download属性值
alink.download = iconMsg.name;
// 触发鼠标点击事件
alink.dispatchEvent(event);
// console.log('下载成功');
}
};
xhr.send();
},
借鉴别人的代码 项目随笔~