首页 > 其他 > 详细

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

时间:2020-12-04 21:40:55      阅读:286      评论:0      收藏:0      [点我收藏+]

后端

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‘}这个一定要传,不然下载的文件就是空白文件

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

原文:https://www.cnblogs.com/xiaofeilin/p/14087440.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!