当前位置:网站首页>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: 协议?
边栏推荐
- Multi thread synchronous downloading of network pictures
- MVC source code sharing
- Ppt template crawler case
- 数据可视化实战:实验报告
- How to design a good technical scheme
- Introduction to canal deployment, principle and use
- EFK昇級到ClickHouse的日志存儲實戰
- Work accumulation - problems encountered in using ThreadLocal in web requests
- TS泛型在函数、接口、类中使用介绍
- Pytorch uses multi GPU parallel training and its principle and precautions
猜你喜欢
Transformer中的Self-Attention以及Multi-Head Self-Attention(MSA)
[alluxio & Dachang] the original boss direct employment was applied in this way
Message queue - function, performance, operation and maintenance comparison
MySQL-08
成水最多的容器
Efk Upgrade to clickhouse log Storage Reality
Message queuing - omnidirectional comparison
Message queue - message transaction management comparison
Experience the new features of Milvus 2.0 together
How to select and build a real-time data warehouse scheme
随机推荐
Typora activation method
Unsatisfied dependency expressed through field ‘baseMapper‘; nested exceptio
Play with a variety of application scenarios and share secrets with Kwai MMU
Zotero使用之自定义参考文献格式
Implement the runnable interface
低代码实时数仓构建系统的设计与实践
Logstash——使用throttle过滤器向钉钉发送预警消息
Logstash - logstash sends an alarm email to email
Connexion et déconnexion TCP, détails du diagramme de migration de l'état
Data visualization practice: Experimental Report
获取当前月份的第一天和最后一天,上个月的第一天和最后一天
Redis multithreading and ACL
消息队列-消息事务管理对比
事务与消息语义
TS泛型在函数、接口、类中使用介绍
Self attention and multi head self attention (MSA) in transformer
Alarm operation and Maintenance Center | build an efficient and accurate alarm collaborative processing system
Dpdk - tcp/udp protocol stack server implementation (II)
Market trend report, technical innovation and market forecast of China's valeryl chloride
如何让主线程等待子线程执行完毕后再执行