vue.js javascript 前端 vue zip文件上传、zip下载 - appendChild(a)

📅 2026-12-01 👤 拼多多助力营销技巧 📂 拼多多助力神器 👁️ 235 次阅读

import JSZip from "jszip";

// 引入UUID

import { uuid } from "vue-uuid";

// 引入zip上传工具函数

import zipUpload from "../util/zipUpload";

data(){

return{

zip: new JSZip(),

}

}

//插播一条--uuid使用

//this.$set(this.listItem, "_id", uuid.v4().split("-").join(""));

//循环要放进去的目录树文件

for (let i = 0; i < this.list.length; i++) {

this.zip.file(this.list[i]._id, this.[i].file, {

base64: true,

});

}

//txt文件

this.zip.file("aaa.txt", JSON.stringify(this.txtList));

this.zip.generateAsync({ type: "blob" }).then((content) => {

// fileSaver(content, "example.zip");

// 打包zip上传

zipUpload(content, this.auth).then((res) => {

// console.log(res);

axios

.post(`url`, res.uploadfile,

{

...res.config,

timeout: 1000 * 60 * 3,

}

)

.then((res) => {

// console.log(res);

if (res.data[0].success == true) {

//执行成功

//...

} else {

//执行失败

//...

}

})

.catch(() => {

this.$alert("上传失败,请检查文件是否过大,网络是否良好", "提示", {

confirmButtonText: "确定",

callback: (action) => {

this.closeLoading(); 拼多多帮助力入口

},

});

});

});

});

//下载zip文件,把zip文件放在了public/static/xxx.zip目录下

downloadScanFile(){

var a = document.createElement("a"); //创建一个标签

const random = Math.random(); // 生成一个随机数, 使用随机数作为参数拼接下载链接

a.href = `./static/xxx.zip?random=${random}`; // './' 是为了打包之后可以找到该文件

a.download = "xxx.zip"; //设置下载文件文件名(可以设置中文名),这里加上.xlsx指定文件类型,pdf文件就指定.fpd即可

a.style.display = "none"; // 障眼法藏起来a标签

document.body.appendChild(a); // 将a标签追加到文档对象中

a.click(); // 模拟点击了a标签,会触发a标签的href的读取,浏览器就会自动下载了

a.remove();

},

//zipUpload.js

/**

* @author liuwei

* @param content jszip文件内容

* @param auth 账号和密码组合加密

* @returns Promise

*/

export default function zipUpload(content, auth) {

return new Promise(res => {

const file = new File([content], "example.zip", {

type: "application/x-zip-compressed",

});

let uploadfile = new FormData();

uploadfile.append("file", file);

const config = {

headers: {

"Content-Type": "multipart/form-data",

"Authorization": auth

}

};

res({uploadfile, config})

})

}

参考: 1、JSZip前端上传压缩包,将压缩包里面的文件上传给后端

好文推荐

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧