后端返回文件流,前端blob下載


后端

return response()->download(storage_path('app/public').$file,$filename,array(
                    'Content-Type' => 'application/pdf',
                ));

前端:

downloadPage(id){
      webServices.post('downPage',{"id":id},{responseType:'arraybuffer'}).then((res)=>{
        let blob = new Blob([res.data], { type: "application/pdf" });
        const elink = document.createElement('a')
        elink.download = 'testPage.pdf';
        elink.style.display = 'none'
        elink.href = URL.createObjectURL(blob)
        document.body.appendChild(elink)
        elink.click()
        URL.revokeObjectURL(elink.href) // 釋放URL 對象
        // this.downLoading = false
        document.body.removeChild(elink)

      });
    },
{responseType:'arraybuffer'}這個一定要傳,不然下載的文件就是空白文件


免責聲明!

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



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