后端下發excel流數據,前端如何接收並下載


如果不需要權限,直接通過鏈接下載;

需要權限,則通過ajax請求,拿到流數據
獲取數據的時候,一定要加上(responseType: ‘blob’),表示后台傳過來的數據用 blob 對象接收.

axios.post(`接口路徑`, { responseType: 'blob' });

//下載文件的方法

downfile(fileName, data) {
                 if ("msSaveOrOpenBlob" in navigator) {
                let blob = new Blob([data]);
                window.navigator.msSaveOrOpenBlob(blob, fileName);
              } else {
                   let blob = new Blob([data], { type: "application/vnd.ms-excel" });
                   let fileUrl = URL.createObjectURL(blob);
                   let a = document.createElement("a");
                  a.setAttribute("href", fileUrl);
                  a.setAttribute("download", fileName);
                  a.click();
             }
}

調用downfile方法下載Excel文件

(fileName: 文件名.xlsx  data:后台返回的數據)


免責聲明!

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



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