axios获取文件流并下载文件

// 加载中效果开始
loadingInstance.start();
let  { fileRoute, fileName} = item;
let token = "Bearer " + JSON.parse(window.localStorage.getItem('access_token')).v;
axios.defaults.headers.common["Authorization"] = token;
axios({
  method: "get",
  responseType: 'blob',
  url: '/apollosrv/api/dlCenter/v1.0/download',
  contentType: "application/json;charset=UTF-8",
  params: {
    fileRoute: fileRoute,
    fileName: fileName
  }
}).then(res => {
  if(res.status == "200") {
    const aLink = document.createElement("a");
    let blob = new Blob([res.data], {type: "text/plain;charset=utf-8"});
    aLink.href = URL.createObjectURL(blob)
       aLink.setAttribute('download',fileName) // 设置下载文件名称
       aLink.click();
       URL.revokeObjectURL(aLink.href)
    // 加载中效果结束 loadingInstance.finish(); document.body.appendChild(aLink);   }
else{
    // 加载中效果结束     loadingInstance.finish();
    this.$Message.error("系统异常,下载失败");   } });

 

posted @ 2021-10-26 15:49  吃饭睡觉打痘痘  阅读(505)  评论(0编辑  收藏  举报