vue zip文件上传、zip下载

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"); //创建一个<a></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前端上传压缩包,将压缩包里面的文件上传给后端

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值