后端返回文件流,前端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