当前位置: 首页 > news >正文

在线预览PDF文件、图片,并且预览地址不显示文件或图片的真实路径。

实现在线预览PDF文件、图片,并且预览地址不显示文件或图片的真实路径。

    • 1、vue使用blob流在线预览PDF、图片(包括jpg、png等格式)。
      • 1、按钮的方法:
      • 2、方法详细:(此方法可以在发起请求时携带token,适合JWT认证)
      • 3、script中定义一些指定格式使用blob转换的文件类型:
      • 4、export default 中data ( ) 设置文件类型数组包含何种文件格式:
    • 2、springboot后端接口返回文件流。(文件类型放在请求头,以便前端获取处理)
      • 1、方法1:
      • 2、方法2:
    • 3、前端最终的展示效果:

1、vue使用blob流在线预览PDF、图片(包括jpg、png等格式)。

想要实现在线预览PDF文件、图片,并且预览地址不显示文件或图片的真实路径,可使用blob流。

vue前端关键代码:

1、按钮的方法:

<el-button color="#00ffcc" size="small" @click="invoicePreview(props.row.oid)" v-if="props.row.ifInvoice == 1"><el-icon><Tickets /></el-icon>查看发票</el-button>

2、方法详细:(此方法可以在发起请求时携带token,适合JWT认证)

// 预览发票
invoicePreview(oid){console.log(oid)this.$http({url: 'invoicePreview',method: 'get',responseType: 'arraybuffer',params: {oid: oid}}).then(res => {console.log(res);console.log(res.headers.type);let fileType = res.headers.type;let currType = '';const binaryData = [];if(fileType){currType = fileTypeMap[fileType]if(this.imageType.includes(fileType)){// 识别为图片binaryData.push(res.data);//获取blob链接this.URL = window.URL.createObjectURL(new Blob(binaryData, {type: currType}));window.open(this.URL);}else if(this.wordType.includes(fileType)){// 识别为PDFbinaryData.push(res.data);//获取blob链接this.URL = window.URL.createObjectURL(new Blob(binaryData, {type: currType}));window.open(this.URL);}else {this.$message.error('不支持此文件预览')}}else {this.$message.error('不支持此文件预览')}})}

3、script中定义一些指定格式使用blob转换的文件类型:

const fileTypeMap = {"pdf": "application/pdf","png": "image/png","gif": "image/gif","jpeg": "image/jpeg","jpg": "image/jpeg","txt": "txt/plain",}

4、export default 中data ( ) 设置文件类型数组包含何种文件格式:

作用是与blob转换的文件类型进行匹配。

data () {return {wordType:['pdf', 'txt'],imageType: ['png', 'gif', 'jpeg', 'jpg']}

在这里插入图片描述

2、springboot后端接口返回文件流。(文件类型放在请求头,以便前端获取处理)

后端关键代码:(二选一即可)

1、方法1:

        File file = new File(filePath);//获取文件后缀String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());//使用流的形式下载文件--方法2try (InputStream is = new FileInputStream(filePath);OutputStream os = response.getOutputStream()) {byte[] bytes = StreamUtils.copyToByteArray(is);response.reset();response.addHeader("Content-Disposition", "inline;filename=" + new String(filePath.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));response.addHeader("Content-Length", "" + bytes.length);// 文件类型放在请求头,以便前端获取处理response.addHeader("type", suffix);os.write(bytes);os.flush();} catch (Exception e) {e.printStackTrace();}

2、方法2:

        File file = new File(filePath);//获取文件后缀String suffix = file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length());try {response.addHeader("Content-Disposition", "inline; filename*=UTF-8''" + URLEncoder.encode(file.getName(), "UTF-8"));// 文件类型放在请求头,以便前端获取处理response.addHeader("type", suffix);} catch (UnsupportedEncodingException e) {e.printStackTrace();}ServletOutputStream os = null;try{os = response.getOutputStream();os.write(FileUtils.readFileToByteArray(file));os.flush();} catch (IOException e) {e.printStackTrace();}finally {if(os != null){// 此处的close为一个私有方法close(os);}}
private void close(Closeable closeable){if(closeable != null){try{closeable.close();}catch (IOException e) {e.printStackTrace();}}}

3、前端最终的展示效果:

在这里插入图片描述
在这里插入图片描述
至此完结!

http://www.lryc.cn/news/5837.html

相关文章:

  • Allegro如何设置导入Subdrawing可自由选择目录操作指导
  • SpirngMVC执行原理--自学版
  • 获取savemodel的输入输出节点
  • 《Learning to Reconstruct Botanical Trees from Single Images》学习从单幅图像重建植物树
  • vant 4 正式发布,支持暗黑主题,那么是如何实现的呢
  • MySQL的复制 二
  • 秒杀项目之秒杀商品展示及商品秒杀
  • 教育行业需要什么样的数字产品?
  • Spring MVC
  • 类与对象(上)
  • 正确安装 torch_geometric库
  • 【Unity VR开发】结合VRTK4.0:自身移动(滑动)
  • G1垃圾回收器详解
  • tws耳机哪个牌子音质好?tws耳机音质排行榜
  • TIA博途中DB数据块清零的具体方法示例
  • iptables防火墙屏蔽指定ip的端口
  • JavaScript Math(算数) 对象
  • 超详细的JAVA高级进阶基础知识04
  • Python 运算符?
  • linux nuxt 部署 问题汇总
  • C++内存管理
  • 电子招投标系统源码之 —采购数字化转型快人一步,以大数据支撑供应链管理未来
  • ie获取cookie数据,中文乱码;cookie中文乱码终极解决办法
  • day16_关键字this和super丶就近原则和追根溯源原则
  • MySQL 共享锁 (lock in share mode),排他锁 (for update)
  • 类与对象(下)
  • feign技巧 - form方式传值
  • MATLAB | 情人节来绘制更立体的玫瑰花吧
  • 【Python表白代码】 2.14“Valentine‘s Day”“没别的意思 就是借着特殊日子说声喜欢你”你在哪儿?我去见你~(各种玫瑰源码合集)
  • 压力应变电桥信号隔离放大变送器差分输入0-±10mV/0-±20mV转0-20mA/0-10v