当前位置:网站首页>js-下载图片
js-下载图片
2022-06-26 06:19:00 【YogaMiller】
fucntion downloadIamge(imgsrc, name) {
let image = new Image();
// 解决跨域 Canvas 污染问题
image.setAttribute("crossOrigin", "anonymous");
image.onload = function() {
let canvas = document.createElement("canvas");
canvas.width = image.width;
canvas.height = image.height;
let context = canvas.getContext("2d");
context.drawImage(image, 0, 0, image.width, image.height);
let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
let a = document.createElement("a"); // 生成一个a元素
let event = new MouseEvent("click"); // 创建一个单击事件
a.download = name || "photo"; // 设置图片名称
a.href = url; // 将生成的URL设置为a.href属性
a.dispatchEvent(event); // 触发a的单击事件
};
image.src = imgsrc;
}
// 兼容性不好,QQ浏览器不行, 64版本谷歌都不行
function downloadImg(src, name){
let canvas = document.createElement('canvas'),
img = document.createElement('img');
img.src = src;
img.setAttribute("crossOrigin",'Anonymous')
img.onload = function(e) {
canvas.width = img.width;
canvas.height = img.height;
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0, img.width, img.height);
canvas.getContext('2d').drawImage(img, 0, 0, img.width, img.height);
canvas.toBlob( (blob) => {
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = '文件名';
link.click();
}, "image/jpg");
}
}
1. data: 协议 base64?
2. blob: 协议?
边栏推荐
- 数据可视化实战:数据可视化
- Five solutions across domains
- PyTorch使用多GPU并行训练及其原理和注意事项
- Gof23 - prototype mode
- Failed to configure a DataSource: ‘url‘ attribute is not specified and no embedded datasource could
- MYSQL索引不生效的原因
- Message queue - function, performance, operation and maintenance comparison
- PyTorch混合精度原理及如何开启该方法
- 个人博客系统需求分析
- [spark] how to implement spark SQL field blood relationship
猜你喜欢
随机推荐
Install pyinstaller
Transformer中的Self-Attention以及Multi-Head Self-Attention(MSA)
Installing rainbow in various kubernetes with Helm
Mysql-10 (key)
【微服务系列】Protocol buffer动态解析
China micronutrient market trend report, technical innovation and market forecast
TS泛型在函数、接口、类中使用介绍
Laravel 实现 groupBy 查询分组数量
Zotero文献管理工具之Jasminum(茉莉花)插件
Gof23 - abstract factory pattern
3.pyinstaller模块介绍
Web components series (10) -- realize the basic layout of mycard
SQL server functions
Zotero使用之自定义参考文献格式
[spark] how to implement spark SQL field blood relationship
Play with a variety of application scenarios and share secrets with Kwai MMU
Market trend report, technical innovation and market forecast of microencapsulated chemical pesticides in China
【Spark】Spark SQL 字段血缘如何实现
Pytorch mixing accuracy principle and how to start this method
MySQL 索引底层原理









