vue+element 下载压缩包和导出

在这里插入图片描述

export function goodsInspectionReportDwnloadZip (params) {
  return axios({
    url: "/warehouse-entry-server/v1/goodsInspectionReport/downloadZip",
    method: "get",
    params,
    responseType: "blob"
  })
} 


 //下载
            handleDownloadFile() {
                if (!this.$refs.dragTable.selection.length) {
                    this.$message.warning("请选择数据!");
                } if (this.$refs.dragTable.selection.length > 1) {
                    this.$message.warning("请选择一条数据!");
                } else {
                    let talbeData = this.$refs.dragTable.selection
                    goodsInspectionReportDwnloadZip({ id:talbeData[0].id }).then((res) => {
                        const { code, msg } = res.data;
                        const title = code === 200 ? "操作成功" : "操作失败";
                        const type = code === 200 ? "success" : "error";
                        let blob = new Blob([res.data], { type: "application/zip" });
                        // 设置下载的内容以及格式,zip文件必须设置type: "application/zip" 
                        const url = window.URL.createObjectURL(blob); // 设置路径
                        const link = window.document.createElement("a"); // 创建a标签
                        link.href = url;
                        link.download = "资质编号"+talbeData[0].typeCode+"报告压缩包.zip"; // 设置文件名
                        link.style.display = "none";
                        link.click();
                        URL.revokeObjectURL(url); // 释放内存
                        this.$refs.dragTable.clearSelection();
                    });

                }
            },
   // 导出方法
    handledownload(api) {
      api({
        ...this.page,
        param: this.search
      }).then((res) => {
        const src = URL.createObjectURL(new Blob([res.data]));
        const link = document.createElement("a");
        link.href = src;
        let filename = decodeURIComponent(
          res.headers["content-disposition"].split("filename=")[1]
        );
        link.setAttribute("download", filename);
        document.body.appendChild(link);
        link.style.display = "none";
        link.click();
      });
    },

你可能感兴趣的:(vue.js,javascript,前端)