axios獲取文件流並下載文件


首先給axios設置 responseType:'blob'

下載方式:一、使用a標簽下載

axios.post(url,data,{responseType:'blob'}).then(res => {
    const blob = new Blob([res.data]);//處理文檔流
    const fileName = '資產列表.xlsx';
    const elink = document.createElement('a');
    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);
})

下載方式:二、使用fileDownload插件下載

git地址:https://github.com/kennethjiang/js-file-download

 Axios.get(url, {
    responseType: 'blob',
  }).then(res => {
    fileDownload(res.data, '測試.xlsx');
  });


免責聲明!

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



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