vue 下载文件压缩包

973 阅读1分钟
   /**
     * @param {object[]} imglist
     * @param {string} imglist[].name
     * @param {string} imglist[].url
     * 下载文件列表压缩包
     */
    downloadWithCompress (imglist, zipName) {
      const JSZip = require('jszip');
      const { saveAs } = require('file-saver');
      const zip = new JSZip();
      const promises = [];
      imglist.forEach((item) => {
        const fileData = this.$axios.get(item.url, {
          responseType: 'blob',
          withCredentials: false
        }).then((res) => {
          zip.file(item.name, res.data);
          return res;
        }).catch(() => {
          return false;
        });
        promises.push(fileData);
      });
      Promise.all(promises).then(() => {
        zip.generateAsync({ type: 'blob' }).then((content) => {
          // 生成二进制流
          saveAs(content, '' + zipName + '.zip'); // 利用file-saver保存文件
        });
      });
    },