1. 程式人生 > 其它 >axios獲取檔案流並下載檔案

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("系統異常,下載失敗");   } });