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');
    },
相关推荐
用户6981354491061几秒前
three.js绘制中国地理数据
前端
爱学习的茄子1 分钟前
JavaScript闭包实战:防抖的优雅实现
前端·javascript·面试
前端付豪1 分钟前
9、前端日志埋点系统的架构设计
前端·javascript·架构
张志鹏PHP全栈1 分钟前
TypeScript 第四天,TypeScript的编译选项(一)
前端·typescript
源猿人2 分钟前
基于 Cornerstone.js 的 Vue2 DICOM 医学图像浏览器开发实录
前端·javascript
风舞4 分钟前
HTML核心概念及代码示例的梳理
前端
1君1103565 分钟前
粗解前端Proxy和defineProperty以及他们的区别
前端
Shinpei5 分钟前
如何优雅的在AI应用中渲染Markdown数据
前端·deepseek
Codebee5 分钟前
OneCode List 组件技术解析:设计理念、子类体系与扩展实践
前端
张旭超5 分钟前
vue3 + element-plus el-table表格二次封装,支持复选框,排序,分页。
前端·vue.js