首页 > 其他分享 >js 通过图片链接下载到本地

js 通过图片链接下载到本地

时间:2023-03-01 16:56:48浏览次数:73  
标签:canvas url image js Link 本地 let 图片链接 height

 1         let image = new Image();
 2         image.setAttribute("crossOrigin", "anonymous");
 3         image.src = url;
 4         image.onload = () => {
 5           let canvas = document.createElement("canvas");
 6           canvas.width = image.width;
 7           canvas.height = image.height;
 8           let ctx = canvas.getContext("2d");
 9           ctx.drawImage(image, 0, 0, image.width, image.height);
10           canvas.toBlob((blob) => {
11             let url = URL.createObjectURL(blob);
12             let Link = document.createElement("a");
13             Link.download = "下载名字";
14             Link.href = url;
15             Link.click();
16             Link.remove();
17             // 用完释放URL对象
18             URL.revokeObjectURL(url);
19           });
20         }

* url 为图片地址 如:https://t7.baidu.com/it/u=816042678,3339142019&fm=193&f.gif

标签:canvas,url,image,js,Link,本地,let,图片链接,height
From: https://www.cnblogs.com/PengZhao-Mr/p/17168847.html

相关文章