下载文件流

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 = row.fileName;
          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.$message.success('下载成功')
          console.log(res);
        },
        err => {
          this.$message.error(err.msg)
          throw err;
        }
      );
    },

  下载这种文件流的方式

猜你喜欢

转载自www.cnblogs.com/js-liqian/p/11797302.html
今日推荐