vue下載excel權限限制處理


 在開發過程中會遇到下載excel但是需要傳token來驗證的問題,一般情況下是用直接指向地址來實現,但是token校驗沒辦法實現,所以就用到了blod

 一般情況下:

var pathurl = process.env.VUE_APP_CUSTOMERRL_URL + 'xxx/xxxx';
window.open(pathurl, '_blank')

采用blod:

var xhr = new XMLHttpRequest();
var formData = new FormData(); xhr.open('get',process.env.VUE_APP_CUSTOMERRL_URL + 'xxx/xxxx'); xhr.setRequestHeader("Authorization",'bearer '+localStorage.getItem("access_token"));  xhr.responseType = 'blob'; xhr.onload = function (e) { let blob = new Blob([xhr.response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'}); let downloadElement = document.createElement('a'); let href = window.URL.createObjectURL(blob); //創建下載的鏈接 downloadElement.href = href; downloadElement.download = 'demo.xlsx'; //下載后文件名 document.body.appendChild(downloadElement); downloadElement.click(); //點擊下載 document.body.removeChild(downloadElement); //下載完成移除元素 window.URL.revokeObjectURL(href); //釋放掉blob對象 }; xhr.send(formData);

 


免責聲明!

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



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