欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 维修 > <好记性不如烂笔头系列>vue生成二维码/打包下载

<好记性不如烂笔头系列>vue生成二维码/打包下载

2025/9/3 7:51:17 来源:https://blog.csdn.net/hanchengmei/article/details/145610339  浏览:    关键词:<好记性不如烂笔头系列>vue生成二维码/打包下载
一、生成二维码

1、安装vue-qr
2、引入 import VueQr from ‘vue-qr’

<div ref="boxRef" class="box"><div class="flex"><div class="left">xxxxx</div><div class="right">xxxxxxxx</div></div><VueQr:size="331":logo-margin="5":logo-src="logo":margin="10":text="url"/><div>xxxx</div>
</div>....
components: {VueQr},
....

在这里插入图片描述

二、下载上面整块为单张图

1、安装html2canvas
2、引入import html2canvas from ‘html2canvas’

html2canvas(this.$refs.boxRef).then((canvas) => {const link = document.createElement('a')link.href = canvas.toDataURL('image/png')link.download = `${名称}.png`link.click();alert('下载成功')
}).catch((res) => {alert('下载失败')});
三、下载多张并打包

1、下载 jszip、file-saver

import JSZip from 'jszip'
import FileSaver from 'file-saver'onClickDown() {let list = [{name: '文件名1',url: 'xxx' // 生成二维码的url},{name: '文件名2',url: 'xxx'}]const arr = []for (let i = 0; i < list.length; i++) {const { name, url } = list[i]this.url = urlconst res = await html2canvas(this.$refs.boxRef)const url = res.toDataURL('image/png');arr.push({fileName: name,filePath: url})}this.saveFile(arr)
}setBase64ImgtoFile(filePath, filename) {return new Promise((resolve, reject) => {const image = new Image();image.src = filePath;image.onload = () => {const canvas = document.createElement('canvas');canvas.width = image.width;canvas.height = image.height;const context = canvas.getContext('2d');context.drawImage(image, 0, 0);canvas.toBlob((blob) => {const file = new File([blob], `${filename}.png`, {type: 'image/png'});resolve(file);}, 'image/png');};image.onerror = (error) => reject(error);});}saveFile(list) {const zip = new JSZip()const cache = {}const promises = []list.forEach(val => {letbase64ListRes = []base64ListRes = this.setBase64ImgtoFile(val.filePath, val.fileName).then(data => {zip.file(val.fileName + '.png', data, { binary: true }) cache[val.fileName] = data})promises.push(base64ListRes)})Promise.all(promises).then(() => {zip.generateAsync({ type: 'blob' }).then(content => {FileSaver.saveAs(content, '打包下载.zip'); alert('下载成功')})}).catch((res) => {alert('下载失败')})
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词