使用vue预览PDF

效果图
在这里插入图片描述

讲解之前先看案例,强烈推荐此项目vue-pdf.js-demo
在这里插入图片描述

前端:
方法

filePreview(row){

        if (row.projectFeasibilityAnalysis != null ){
          let name = row.projectFeasibilityAnalysis[0].label
          let url = row.projectFeasibilityAnalysis[0].value
          if (name.lastIndexOf(".pdf") < 0){
            this.$message.warning("不是pdf文件,暂无法预览")
            return;
          }
          console.log()
          handleDownByUrlByPost2("/admin/sys-file/getFileByBucket", name, {"fileName": url}).then((response) => {
            const content = response.data
            const blob = new Blob([content])
            var url = window.URL.createObjectURL(blob)
            window.open('/pdf/web/viewer.html?file=' + encodeURIComponent(url))
          }).catch(response => {
            console.log(response)
          })
        }else {
          this.$message.error("没有附件")

        }
      },

window.open(’/pdf/web/viewer.html?file=’ + encodeURIComponent(url))

特别说明一下这句,encodeURIComponent这个方法是pdf.js提供的,
/pdf/web/viewer.html?file='这个路径我是放在的public里,所以没有加public路径,如果你放在了static里面需要加static,如下:

/static/pdf/web/viewer.html?file=

API

export function handleDownByUrlByPost2(fileUrl,filename,query) {
  return request({
    url: fileUrl,
    method: 'post',
    responseType: 'blob',
    params: query
  })
}

后端:

@PostMapping("/getFileByBucket")
	public void getFileByBucket(String fileName, HttpServletResponse response) {
		fileName = "d://xxx.pdf";
		File file = new File(fileName);
		byte[] data = null;
		try {
			FileInputStream fileInputStream = new FileInputStream(file);
			data = new byte[fileInputStream.available()];
			fileInputStream.read(data);
			response.getOutputStream().write(data);
			fileInputStream.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值