js之 实现浏览器下载图片保存到本地

参考
https://blog.csdn.net/weixin_44104809/article/details/104964261
https://blog.csdn.net/wangchaoqi1985/article/details/107725687

方法

downloadImage(){
 	// 将链接地址字符内容转变成blob地址, 解决图片下载直接打开问题
 	// url 为图片地址
 	const url="https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png"
	fetch(url).then(async res => await res.blob()).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();
	  // 移除元素
	  document.body.removeChild(a);
	})  
}
    原文作者:灼灼桃花夭
    原文地址: https://blog.csdn.net/qq_39835505/article/details/121284383
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞