vue2 结合后端预览pdf 跨域的话就得需要后端来返回 然后前端呈现

javascript 复制代码
<el-button :loading="pdfIslock" v-if="isPDFFile(form.pic)" type="primary" style="margin: 15px 0" @click="previewPDF(form.pic)"> 
  预览pdf
</el-button>

//npm install pdfjs-dist //如果没有就得先安装

import pdf from 'vue-pdf';
components: {
    pdf,
  },

data(){
    return {
      pdfIslock: false,
      pdfUrl: "",
      showDialog: false,   
    }
}

previewPDF(url){
      if(this.pdfIslock){
        this.$message.warning("文件下载中")
        return
      }
      this.pdfIslock = true      
      if(!url){
        this.$message.error("请先上传文件")
        return
      }
      const that = this
      let params = {
        fileName: url.split("/").pop()
      }
//fileProxyPdf 是后端的接口 返回的是一个pdf
      fileProxyPdf(params).then((response) => {      
        const blob = new Blob([response.data], { type: 'application/pdf' });
        const ourl = window.URL.createObjectURL(blob);
        // 将Object URL赋值给pdfUrl
        this.pdfUrl = ourl
        this.showDialog = true    
        this.pdfIslock = false      
      }).catch(function(){
        that.$message.error("下载失败,请稍后重试")
        that.pdfIslock = false 
      });      
      //window.open(url, '_blank');
    },
相关推荐
万少3 小时前
HarmonyOS 开发必会 5 种 Builder 详解
前端·harmonyos
橙序员小站6 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
炫饭第一名8 小时前
速通Canvas指北🦮——基础入门篇
前端·javascript·程序员
王晓枫8 小时前
flutter接入三方库运行报错:Error running pod install
前端·flutter
符方昊8 小时前
React 19 对比 React 16 新特性解析
前端·react.js
ssshooter8 小时前
又被 Safari 差异坑了:textContent 拿到的值居然没换行?
前端
曲折9 小时前
Cesium-气象要素PNG色斑图叠加
前端·cesium
Forever7_9 小时前
Electron 淘汰!新的桌面端框架 更强大、更轻量化
前端·vue.js
Angelial9 小时前
Vue3 嵌套路由 KeepAlive:动态缓存与反向配置方案
前端·vue.js
jiayu9 小时前
Angular学习笔记24:Angular 响应式表单 FormArray 与 FormGroup 相互嵌套
前端