PDF 直接下载问题 egg.js vue

PDF直接下载问题

浏览器中,pdf文件开发为预览而非直接下载

pdf文件与页面 不存在跨域问题,解决方案

handleDownload (record) {
      var oReq = new XMLHttpRequest()
      var URLToPDF = record.url
      oReq.open('GET', URLToPDF, true)
      oReq.responseType = 'blob'
      oReq.onload = function () {
        var file = new Blob([oReq.response], {
          type: 'application/pdf'
        })
        const url = window.URL.createObjectURL(file)
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', `${record.name}_${moment().format('YYYYMMDD')}.pdf`)
        document.body.appendChild(link)
        link.click()
      }
      oReq.send()
    }

pdf文件与页面 存在跨域问题,解决方案

egg.js 将文件地址转成文件流
  async pdfToBlob() {
    const { ctx } = this;
    const params = ctx.request.body;
    const result = await this.ctx.curl(params.url);
    ctx.set('Content-Type', 'application/pdf')
    ctx.body = result.data;
  }
vue 下载文件
downloadPdf({ 'url': record.url }).then(res => {
        const BLOB = new Blob([res])
        const url = window.URL.createObjectURL(BLOB)
        const link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', `***`)
        document.body.appendChild(link)
        link.click()
})



```javascript
// 下载pdf
export function downloadPdf (parameter) {
  return axios({
    url: '***',
    method: 'post',
    data: parameter,
    responseType: 'blob'
  })
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值