blob文件流前端显示pdf三种方法

教程下载地址: 网赚博客https://www.piaodoo.com/创业项目排行榜前十名https://www.piaodoo.com/


首先请求需要修改

responseType: ‘blob’, 需要修改

请求头
{
        responseType: 'blob',
        url: url,
        method: 'get',
    }

三种方法:

1.直接处理,在新页面打开

const blob = new Blob([data],{
type:'application/pdf'
})
let url = window.URL.createObjectURL(blob)
window.open(url,'_blank')
问题在于父页面关闭或者刷新后,文件页面获取不到文件流,刷新显示空白页。

2.在新页面用iframe接

<iframe :src='xxxxxx'>
问题在于点击iframe中文件之后无法在iframe监听事件,ctrl+p 显示空白

3.使用pdf.js

到 mozilla.github.io/pdf.js/gett… 页面中找到下载位置,下载 PDF.js
在viewer.js 修改
注释下列代码   不然 可能会出现跨域错误,无法正常预览文件
if (origin !== viewerOrigin && protocol !== "blob:") {
  throw new Error("file origin does not match viewer's");
}

随后在页面展示
let path = window.URL.createObjectURL(blob)
const fileUrl = ‘/pdfjs2/web/viewer.html’
// 生产环境下
if (process.env.NODE_ENV === ‘production’) {
this.pdfurl = fileUrl + ‘?file=’ + encodeURIComponent(path)
} else {
// 开发环境
this.pdfurl = fileUrl + ‘?file=’ + encodeURIComponent(path)
}

修改清晰度 --注意清晰度越高,打印预览时 谷歌内核滚动条越卡
this._printResolution = 450//printResolution || 150
新版本的pdf.js viewer.js被改为mjs,上线时nginx需要修改
另外还有个bug 在一个页面打印预览时,同源的其他页面无法点击

附:Blob流在线预览PDF文件、图片

这个要注意格式,要加上responseType: 'arraybuffer'

import axios from 'axios'
const fileTypeList = ['application/pdf', 'image/png', 'image/gif', 'image/jpeg', 'txt/plain']
invoicePreview () {
      axios({
        method: 'get',
        url: '/acc_test/index/test_pdf',
        baseURL: process.env.HOSTURL,
        responseType: 'arraybuffer'
      }).then(res => {
        let fileType = res.headers['content-type']
        const binaryData = []
    if (fileType &amp;&amp; fileTypeList.includes(fileType)) {
      binaryData.push(res.data)
      let URL = window.URL.createObjectURL(new Blob(binaryData, { type: fileType, charset: 'utf-8' }))
      window.open(URL)
    } else {
      this.$Message.error('不支持此文件预览')
    }
  })
}</pre></div>

总结 

到此这篇关于blob文件流前端显示pdf三种方法的文章就介绍到这了,更多相关blob文件流显示pdf内容请搜索网赚博客https://www.piaodoo.com/以前的文章或继续浏览下面的相关文章希望大家以后多多支持网赚博客https://www.piaodoo.com/!

                        友情连接:  

茂名一技http://www.szsyby.net/


茂名一技http://www.enechn.com/


美文集http://www.tpyjn.cn/


手游排行前十名http://www.bjkhrx.com/


蔚来汽车http://www.weilaiqiche.cn/

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值