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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM