export const exportImg = function (imgsrc,name) { /* url图片地址,name图片名称 */
// 不跨域
// let alink = document.createElement('a');
// alink.href = url;
// alink.download = name || '图片下载'; //图片名
// window.open(alink);
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;
};
this.exportImg(url,'图片')
// url格式如http://192.168.0.0:8080/services/school/api/si-file-infos/get-img?imgUrl=C:/Users/Administrator.USER-20170313WG/AppData/Local/Temp/undertow-docbase.4291210821146122927.8085/upload/image/1574317025041001.png