这边是直接下载,要点击下载的可以把函数绑在触发事件上:
dataURLtoBlob=(dataurl) => {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
//下载图片到本地
downloadFile=(url, name) => {
var a = document.createElement('a');
a.setAttribute('href', url);
a.setAttribute('download', name);
a.setAttribute('target', '_blank');
let clickEvent = document.createEvent('MouseEvents');
clickEvent.initEvent('click', true, true); //模拟点击
a.dispatchEvent(clickEvent);
}
downloadFileByBase64=(base64, name) => {
var myBlob = this.dataURLtoBlob(base64);
var myUrl = URL.createObjectURL(myBlob);
this.downloadFile(myUrl, name); //base64:传入base64 name:为下载图片名字,自定义
}
.......
this.downloadFileByBase64(base64, name) //调用