跳转至

二进制文件流下载

methods: {
  // 下载方法
  downloadMethod(params, saveName) {
    sysFileDownload(params, { responseType: "blob" }).then((res) => {
      if (!res) {
        this.$message.warning("文件下载失败");
        return;
      }
      if (typeof window.navigator.msSaveBlob !== "undefined") {
        window.navigator.msSaveBlob(new Blob([res]), saveName);
      } else {
        const url = window.URL.createObjectURL(new Blob([res]), { type: "application/zip" });
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", saveName);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        window.URL.revokeObjectURL(url);
        this.$message.success("文件下载成功");
      }
    });
  },
  // 使用下载
  handleDownFile(record) {
    console.log("record", record);
    console.log(this.selectedRowKeys);
    const params = {
      ids: this.selectedRowKeys.join(",")
    };
    // 判断是否选择了下载文件
    const selectData = this.selectedRowKeys.length;
    if (selectData) 
      // 单文件下载显示文件名
      if (selectData === 1) {
        this.downloadMethod(params, this.selectedRows[0].fileName);
      } else {
      // 多文件下载重命名
        this.downloadMethod(params, "download.zip");
      }
    } else {
      this.$message.warning("请先选择要下载的文件");
    }
  },
}