逻辑:
先将convas转换为base64,然后将base64转换下载
//转换为base64
const base64 = canvas.toDataURL();
定义下载的方法
const downloadBase64Image = (base64Data: any, fileName = '') => {
let arr = base64Data.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
let blob = new Blob([u8arr], { type: mime });
let objUrl = URL.createObjectURL(blob);
let aTag = document.createElement('a');
aTag.href = objUrl;
aTag.download = fileName;
document.body.appendChild(aTag);
aTag.click();
URL.revokeObjectURL(objUrl);
document.body.removeChild(aTag);
}
调用
// 点击下载二维码
const downloadQrcode = () => {
downloadBase64Image(base64 , '文件名')
}