vue 下載帶有token校驗的文件


正常下載代碼

window.location.href = baseURL + "/api/storage/download?fileName=" + encodeURI(fileName) + "&fileUrl=" + fileUrl;

window.location.href不能帶請求頭,添加參數

帶token代碼

download(row.id).then(response => {

        const { data, headers } = response;
        const fileName = headers['content-disposition'].replace(/\w+;filename=(.*)/, '$1');
        let blob = new Blob([data], {type: headers['content-type']});
        // let objectURL = window.URL.createObjectURL(blob);
        // window.location.href = objectURL;
        console.log(fileName);

        let dom = document.createElement('a')
        let url = window.URL.createObjectURL(blob)
        dom.href = url
        // dom.download = decodeURI(fileName)
        dom.download = decodeURI(row.name+".xls");
        dom.style.display = 'none'
        document.body.appendChild(dom)
        dom.click()
        dom.parentNode.removeChild(dom)
        window.URL.revokeObjectURL(url)
      });

其他方案為嘗試

用axios或者你用的請求庫,配置完header后,get請求到文件,然后用file-saver這個包把拿到的文件保存下來就好了

參考資料


免責聲明!

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



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