导出word、excel、zip、

255 阅读1分钟

功能主要由后端实现,前端负责对接口

导出word

this.$http({
            url: api.xdjcWord,
            method: "get",
            responseType: "blob",
            timeout: 30000,
            params: { id: this.currentId },
          }).then((res) => {
            console.log(res);
            let fileName = decodeURIComponent(
              "xxx-"+ ".doc"
            );//名称
            let blob = new Blob([res.data], {
              //[res],开发过程中要和后台沟通好,返回的文件流是在 res 中还是在 res.data 中。
              type: res.headers["content-type"],
            });
            if (window.navigator.msSaveOrOpenBlob) {
              // 兼容 IE10
              navigator.msSaveBlob(blob, fileName);
            } else {
              let a = document.createElement("a");
              let href = window.URL.createObjectURL(blob); //创建下载的链接
              a.href = href;
              a.download = fileName; //下载后文件名
              document.body.appendChild(a);
              a.click(); //点击下载
              document.body.removeChild(a); //下载完成移除元素 //window.URL.revokeObjectURL(href); //释放掉 blob 对象
            }
          });

导出excel

handleExcel(){
      let params = JSON.parse(JSON.stringify(this.form))
      params.token =   sessionStorage.getItem("token")
      let url = this.$sys.getUrl('api',params)
      console.log(url);
      window.open(
          url,
          '_blank'
      );
    },

this.$sys.getUrl为封装的方法,如下

getUrl(url,data){
    return url += (url.indexOf('?') < 0 ? '?' : '') + this.getParam(data)
  }

导出zip

window.open(
            api.bianRenExport +
              `?id=${
                this.currentRowData.id
              }&token=${sessionStorage.getItem("token")}`
          );