【Vue】后端返回文件流,前端预览文件

javascript 复制代码
let date;
        request({
          url: this.$route.query.url,
          method: 'get',
          responseType: 'blob',
        }).then(resp => {
          date = resp
          this.path = window.URL.createObjectURL(new Blob([resp], {type: "application/pdf"}))
        }).catch((e) => {
          //旧版本浏览器下的blob创建对象
          window.BlobBuilder = window.BlobBuilder ||
            window.WebKitBlobBuilder ||
            window.MozBlobBuilder ||
            window.MSBlobBuilder;
          if (e.name == 'TypeError' && window.BlobBuilder) {
            if (date) {
              BlobBuilder.append(date);
              this.path = URL.createObjectURL(new BlobBuilder().getBlob("application/pdf"))
            }
          } else {
            console.log("浏览器版本较低,暂不支持该文件类型预览");
          }
        }).finally(() => {
          window.URL.revokeObjectURL(this.path);
        })
复制代码
responseType必须设置为blob
html 复制代码
  <iframe style="width: 100%;height: 500px" :src="path"></iframe>

文件预览效果

相关推荐
一颗奇趣蛋几秒前
哨兵模式-无限滚动
前端
爱分享的鱼鱼几秒前
Element Plus 日期选择器(DatePicker)深度解析:从基础用法到高级定制
前端
paterWang4 分钟前
基于SpringBoot+Vue的鞋类商品购物商城系统的设计与实现
vue.js·spring boot·后端
evle18 分钟前
从 Recoil 的兴衰看前端状态管理的技术选型
前端·react.js
恋猫de小郭39 分钟前
Flutter 2026 Roadmap 发布,未来计划是什么?
android·前端·flutter
时清云1 小时前
2025-年终总结
前端
Esaka_Forever1 小时前
Promise resolve 的基础用法
前端·javascript
a1117761 小时前
卡通风格 UI 组件库html (TRIZ UI Kit [特殊字符])
前端·ui·html
鳄鱼杆1 小时前
虚拟机 | 如何通过域名访问虚拟机中的Web服务?
前端
赵_叶紫2 小时前
Vue 3 从基础到组合式 API 全解析
vue.js