vue点击下载图片
<template>
<div>
<a
v-if="!isFirefox"
@click.stop.prevent="handleDownloadQrIMg"
class="qrcode"
>
<img src="static/img/load.png">
</a>
<a
v-if="isFirefox"
:href="prefixBase64 + qrBase64"
download="qrcode.png"
class="qrcode"
>
<img src="static/img/load.png">
</a>
</div>
</template>
<script>
export default {
data() {
return {
qrBase64: "", // 二维码对应的base64(在方法里面进行获取)
prefixBase64: "data:image/png;base64,", // base64前缀
isFirefox: false
};
},
mounted() {
// 判断浏览器是否是火狐
if (navigator.userAgent.indexOf("Firefox") > 0) {
this.isFirefox = true;
}
},
methods: {
// 点击下载图片
handleDownloadQrIMg() {
// 这里是获取到的图片base64编码,这里只是个例子哈,要自行编码图片替换这里才能测试看到效果
const imgUrl = this.prefixBase64 + this.qrBase64;
// 如果浏览器支持msSaveOrOpenBlob方法(也就是使用IE浏览器的时候),那么调用该方法去下载图片
if (window.navigator.msSaveOrOpenBlob) {
let bstr = atob(imgUrl.split(",")[1]);
let n = bstr.length;
let u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
let blob = new Blob([u8arr]);
window.navigator.msSaveOrOpenBlob(blob, "chart-download" + "." + "png");
} else {
// 这里就按照chrome等新版浏览器来处理
let a = document.createElement("a");
a.href = imgUrl;
a.setAttribute("download", "chart-download");
a.click();
}
}
}
};
</script>
本文作者:___mouM
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。
版权说明:本文版权归作者和博客园共有,欢迎转载。但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利.

浙公网安备 33010602011771号