前端文件word Excel pdf PPT预览

组件一

bash 复制代码
<template>
    <j-modal
      :visible="visible"
      :fullscreen="fileType!=='other'&&fileType!=='word'"
      @ok="handleOk"
      :width="1200"
      @cancel="handleCancel"

    >
        <vue-office-docx
          v-if="fileType==='word'"
          :src="fileUrl"
          style="height: 100vh"
          @rendered="renderedHandler"
          @error="errorHandler"
          :request-options="headers"
        />
      <vue-office-excel
          v-if="fileType==='excel'"
          :src="fileUrl"
          style="height: 100%"
          @rendered="renderedHandler"
          @error="errorHandler"
          :request-options="headers"
        />
      <vue-office-pdf
        v-if="fileType==='pdf'"
        :src="fileUrl"
        style="height: 100vh"
        @rendered="renderedHandler"
        @error="errorHandler"
      />
      <div v-if="fileType==='image'">
        <img :src="fileUrl" style="height: 80%;"></img>
      </div>
      <div v-if="fileType==='video'" style="margin:auto 0 ">
        <video :src="fileUrl" :controls="true" style="height: 80%;" :autoplay="false"></video>
      </div>
      <div v-if="fileType==='radio'">
        <audio :src="fileUrl" :controls="true" :autoplay="false"></audio>
      </div>
      <div v-if="fileType==='other'">
        <a-row>
          该文件格式暂不支持预览,请下载后查看
        </a-row>
        <br>
        <a-row>
          <a-button type="primary" @click="downloadFile">下载</a-button>
        </a-row>
      </div>
      <ppt-view :src="fileUrl" v-if="fileType==='pptx'"></ppt-view>
    </j-modal>
</template>
<script>
//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
import VueOfficeExcel from '@vue-office/excel'
import VueOfficePdf from '@vue-office/pdf'
import PptView from './PptView'
//引入相关样式
import '@vue-office/docx/lib/index.css'
import '@vue-office/excel/lib/index.css'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
export default {
  name: 'filePreviewDialog',
  components: {
    VueOfficeDocx,
    VueOfficeExcel,
    VueOfficePdf,
    PptView
  },
  data() {
    return {
      fileType:'',
      fileUrl: '',
      fileName: '',
      visible: false,
      headers: '',
      baseDomain:window._CONFIG['domianURL']+'/sys/common/static/'
    };
  },
  created() {
    const token = Vue.ls.get(ACCESS_TOKEN);
    //---------------------------- begin 图片左右换位置 -------------------------------------
    this.headers = {"X-Access-Token":token};
  },
  methods: {
    init(file) {
      this.fileUrl=this.baseDomain+file.fileUrl;
      this.fileName=file.fileName
      this.fileType=file.fileSuffix
      console.log("文件地址:", this.fileUrl,"文件名字", this.fileName, "文件类型", this.fileType)
    },
    renderedHandler() {
      console.log("渲染完成")
    },
    errorHandler(e) {
      if (e.status == 404) {
        this.$message.error("该文件未找到!")
      }
    },

    handleOk() {
      this.visible = false;
    },
    handleCancel() {
      this.visible = false;
    },
    downloadFile() {
      const tag = document.createElement("a");
      tag.style.display = 'none';
      tag.target = '_blank';
      tag.href = this.fileUrl;
      tag.download = this.fileName;
      document.body.appendChild(tag);
      tag.click();
      document.body.removeChild(tag);
    }

  },

}
</script>

<style scoped>
/deep/ .ant-modal-footer{
  display: none;
}
.vue-office-docx .docx-wrapper>section.docx {
  margin-bottom:5px;
  width: 50vw!important;
}
</style>

PPT组件二

1.在pptx.js官网下载压缩包
pptx

(2)选择版本下载

2.在public文件夹中添加pptxjs依赖文件

3.在index.html中引入

bash 复制代码
    <link rel="stylesheet" href="/PPTXjs/css/pptxjs.css" />
 
    <link rel="stylesheet" href="/PPTXjs/css/nv.d3.min.css" />
    <!-- for charts graphs -->
 
    <script
      type="text/javascript"
      src="/PPTXjs/js/jquery-1.11.3.min.js"
    ></script>
 
    <script type="text/javascript" src="/PPTXjs/js/jszip.min.js"></script>
    <!-- v2.. , NOT v.3.. -->
 
    <script type="text/javascript" src="/PPTXjs/js/filereader.js"></script>
    <!--https://github.com/meshesha/filereader.js -->
 
    <script type="text/javascript" src="/PPTXjs/js/d3.min.js"></script>
    <!-- for charts graphs -->
 
    <script type="text/javascript" src="/PPTXjs/js/nv.d3.min.js"></script>
    <!-- for charts graphs -->
 
    <script type="text/javascript" src="/PPTXjs/js/pptxjs.js"></script>
 
    <script type="text/javascript" src="/PPTXjs/js/divs2slides.js"></script>
    <!-- for slide show -->

4.在页面中使用

bash 复制代码
<template>
  <div id="pptx"></div>
</template>

<script>
export default {
  name: 'PptView',
  mounted() {
    this.pptxShow(this.src);
  },
  methods: {
    pptxShow(src) {
      this.$nextTick(() => {
        $("#pptx").pptxToHtml({
          pptxFileUrl: src,
          slidesScale: "100%",
        });
      });
    },
  },
  props: {
    src: {
      type: String,
      required: true,
    },
  },
}
</script>

<style scoped>

</style>
相关推荐
Highcharts.js1 小时前
React 中原生支持使用 JSX 组件配置自定义表格 Highcharts Grid
前端·javascript·react.js·前端框架·jsx·highcharts·自定义表格
薛定谔的猫-菜鸟程序员2 小时前
Electron开发超详细教程(从入门到放弃)
前端·javascript·electron
最强小杰2 小时前
DeepSeek V4 Flash + Codex CLI 接入教程:原生 Responses API 适配,从配置到跑通收藏这篇就够了(2026)
java·前端·python·ai
静水leo3 小时前
使用 Whistle 拦截接口并模拟后端响应
前端
掘金者阿豪3 小时前
SQL Server数据迁移完成后,凌晨的数据库告警该怎么查?
前端·后端
JudithHuang3 小时前
React 嵌套路由与布局
前端·react.js·arcgis·react·react router
htzyl2063 小时前
前端阶梯——第四十三章、实战二:待办清单To-Do
前端·javascript·html
摹客4 小时前
【3D设计】界面设计中的3D元素:应用场景+5款工具横评(含Three.js)
前端
Hilaku4 小时前
CORS 到底在保护谁?90% 的前端理解错了跨域的本质
前端·javascript·程序员