前端接收到PDF文件流数据预览及浏览器兼容问题

发布于 2022-09-12 01:41:53 字数 1378 浏览 14 评论 0

项目中有一个需求,接收到从后端返回的pdf文件流数据,然后将其展示到网页中,但是遇到了兼容性和文件保存的问题。


相关代码:

//预览pdf文件方法
pdfPreview:  function(blob,filename){
    if(!blob) return
    let url = window.URL.createObjectURL(new Blob([blob]));
    let html = "<embed width='100%' height='100%' name='plugin' id='plugin' type='application/pdf' src='" + url + "'>";
    let newwindow = window.open('','_blank','');
    newwindow.document.write(html);
}

//axios请求
axios({
    method: 'post',
    url: '/repPreview',
    data: {
        report_id: row.report_id
    },
    responseType: 'blob'
}).then(res => {
    if(res.status === 200){
        console.log("预览返回:");
        console.log(res);
        _this.pdfPreview(res.data,res.headers.filename);
    }else{
        _this.$notify({
            type:  'error',
            message:  "未知错误!"
        });
    }
}).catch(err => {console.log(err)});

接口返回:

image.png

谷歌浏览器效果:

image.png
但是此时Ctrl+S无法保存该pdf文件,点击浏览器中自带的下载按钮也无法下载

欧朋浏览器效果:

image.png

IE浏览器效果:

image.png


是否pdf文件流的预览方式不对?该如何解决?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟凡古楼 2022-09-19 01:41:53

参考pdf.js插件解决兼容问题

网名女生简单气质 2022-09-19 01:41:53

请问你解决了这个问题么

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文