下载文件流

Vue.prototype.$xlsx_get = function (url, data, fun, err) {
  axios({
      method: 'get',
      url: url,
      responseType: "blob",
      headers: {
        'Authorization': userName ? userName.token_type + userName.access_token : "Basic emh4eTp6aHh5"
      },
      params: data,
    }).then(function (res) {
      if (fun) {
        fun(res)
      }
    })
    .catch(function (error) {
      if (err) {
        err(error)
      }
    });
}

  

    // 下载
    downLoadClick(row) {
      console.log(row);
      this.$xlsx_get(
        `/dcenter/backup/download/${row.id}`,
        {},
        res => {
         const blob = new Blob([res]);
              console.log(blob);
              const elink = document.createElement("a");
              const fileName = '名字.xlsx';
              elink.download = fileName;
              elink.style.display = "none";
              elink.href = URL.createObjectURL(blob);
              document.body.appendChild(elink);
              elink.click();
              URL.revokeObjectURL(elink.href); // 释放URL 对象
              document.body.removeChild(elink);
              this.isDefault = false
              this.$message.success("导出成功");
          console.log(res);
        },
        err => {
          this.$message.error(err.msg)
          throw err;
        }
      );
    },

  下载这种文件流的方式

 

 

posted @ 2019-11-05 11:19  秋风渡明月  阅读(178)  评论(0编辑  收藏  举报