移动端、H5长按保存到相册、web端长按自行选择保存位置
先上效果图
2.源码
<template>
<div class="save-file">
<div class="img-main" @touchstart="touchStart()" @touchend="touchEnd()">
<img :src="qrcodeUrl" alt="">
</div>
</div>
</template>
export default {
data() {
return {
qrcodeUrl: 'https://t7.baidu.com/it/u=2405382010,1555992666&fm=193&f=GIF',
qrcodeUrl64:'',
timer:''
}
},
methods: {
touchEnd() {
//手指离开
clearTimeout(this.timer);
},
touchStart() {
//手指触摸
clearTimeout(this.timer); //再次清空定时器,防止重复注册定时器
this.timer = setTimeout(() => {
this.downloadIamge(this.qrcodeUrl64, "pic");
}, 1000);
},
downloadIamge(imgsrc, name) {
//下载图片地址和图片名
let image = new Image();
// 解决跨域 Canvas 污染问题
image.setAttribute("crossOrigin", "anonymous");
image.onload = () => {
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;
},
}
}