js实现保存图片到本地
当你想分享你的带二维码的宣传图片给别人的时候,一般要先将文件下载到本地,再去微信、或QQ等软件分享出去。
直接上代码:
// fetch有同源策略,本地调试注意要起服务器
fetch("https://gelimalloss.gree.com/gree-mall-v2/images/comment-icon.png", {
mode: 'cors'
})
.then(async res => {
const e = await res.blob()
return e;
})
.then((blob) => {
const a = document.createElement('a');
a.style.display = 'none';
a.href = URL.createObjectURL(blob);
a.download = 'image.png';
document.body.appendChild(a);
a.click();
setTimeout(() => {
a.remove();
}, 2000)
})
在浏览器的下载栏就会出现下载的图片了