JSZip + FileSaver 实现Vue打包下载图片

发布时间:2024-12-24 05:46

下载常用地图APP,方便实时导航。 #生活知识# #出行攻略#

最新推荐文章于 2024-11-04 16:21:12 发布

灬Change 于 2021-07-09 14:29:35 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

1. 版本

"file-saver": "^2.0.5",
"jszip": "^3.6.0"

2. 实现

import FileSaver from 'file-saver';

import JSZip from 'jszip';

export default class exportUtils {

static downloadZipByImageUrlMap(source, zipName) {

console.log('开始打包');

let doneCache = new Map();

let zip = new JSZip();

for (let dirName of source.keys()) {

let fileList = source.get(dirName);

let base64List = [];

doneCache.set(dirName, base64List);

for (let file of fileList) {

let image = new Image();

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();

canvas.toDataURL('image/png');

base64List.push({

name: file.name,

base64: url.substring(url.indexOf(',') + 1)

});

let ready = true;

for (let key of source.keys()) {

if (doneCache.get(key) === undefined || doneCache.get(key).length < source.get(key).length) {

ready = false;

break;

}

}

if (ready) {

console.log('开始下载');

for (let key of doneCache.keys()) {

let imageList = doneCache.get(key);

let imgDir = zip.folder(key);

for (let k = 0; k < imageList.length; k++) {

if (imageList[k].base64 === null) {

continue;

}

imgDir.file(imageList[k].name + '_' + k + '.png', imageList[k].base64, {base64: true});

}

}

zip.generateAsync({type: 'blob'}).then(function (content) {

FileSaver.saveAs(content, zipName + new Date().getTime() + '.zip');

});

}

};

image.onerror = function () {

for (let dieName of source.keys()) {

let tmpBase64List = doneCache.get(dieName);

for (let file of source.get(dieName)) {

if (image.currentSrc === file.url) {

tmpBase64List.push(file);

}

}

doneCache.set(dieName, tmpBase64List);

}

};

image.src = file.url;

}

}

}

}

' 3. 参考

https://blog.csdn.net/qq_42157868/article/details/107368156

https://blog.csdn.net/qq_43258252/article/details/85063970

网址:JSZip + FileSaver 实现Vue打包下载图片 https://www.yuejiaxmz.com/news/view/551301

相关内容

vue 项目中 动态加载图片src的解决方法
vue如何制作照片墙
Vue项目的打包方式(生成dist文件)
vs code 打造舒适的vue开发环境
vue&Element
基于SpringBoot+Vue的校园二手图书交易平台设计与实现
Springboot+Vue校园二手图书销售系统
基于SpringBoot+Vue的二手图书交易系统设计与实现
Vue驱动,购物新体验:揭秘如何打造高效便捷的在线购物网站
Nodejs+vue+Express校园二手图书网站

随便看看