当前位置:网站首页>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: 协议?
边栏推荐
- 视图简析
- MySQL-07
- SparseArray
- TS泛型在函数、接口、类中使用介绍
- China micro cultivator market trend report, technical dynamic innovation and market forecast
- Volatile application scenarios
- How to select and build a real-time data warehouse scheme
- Getting to know concurrency problems
- MySQL 索引底层原理
- University Information Management System
猜你喜欢

ByteDance starts the employee's sudden wealth plan and buys back options with a large amount of money. Some people can earn up to 175%

TCP連接與斷開,狀態遷移圖詳解

Logstash - logstash pushes data to redis

MVC source code sharing

Prometheus和Zabbix的对比

Library management system

Basic construction of SSM framework

Zotero文献管理工具之Jasminum(茉莉花)插件

Solve the problem that Cmdr cannot use find command under win10

Younger sister Juan takes you to learn JDBC -- two days' Sprint Day2
随机推荐
Everything is a vector. The service practice of iqiyi online vector recall project
volatile应用场景
Requirement analysis of personal blog system
Logstash——使用throttle过滤器向钉钉发送预警消息
Transaction and message semantics
Pytorch mixing accuracy principle and how to start this method
Thinking skills of technical leaders
ts中枚举类型(enum)简单使用
去哪儿网BI平台建设演进史
工作积累——Web请求中使用ThreadLocal遇见的问题
Comparison between Prometheus and ZABBIX
GoF23—原型模式
GoF23—工厂模式
Go语言学习笔记 1.2-变量篇
Go语言学习笔记 1.1
个人博客系统需求分析
Top down transformation method
Work accumulation - problems encountered in using ThreadLocal in web requests
Solve the problem that Cmdr cannot use find command under win10
稀疏数组sparsearray