vue通过iframe预览 pdf、word、xls、ppt、txt文件

vue通过iframe预览 pdf、word、xls、ppt、txt文件

iframe中预览只能直接打开pdf文件,其他文件需要通过office365预览。

效果:

组件代码:

js 复制代码
<!--
 * @fileName: 文件预览-FileView.vue
 * @date: yanghaoxing-2024-08-16 09:32:24
!-->
<template>
  <div class="file-view">
    <iframe v-show="showIframe()"
            :src="getViewFilePath(fileItem)"
            class="previewIframe"></iframe>
    <div class="pt-20"
         v-show="!showIframe()">
      <a-result status="success"
                title="该文件无法预览,已开始自动下载!"
                :sub-title="`文件名:${fileItem.name},文件类型:${fileItem.type}`">
      </a-result>
    </div>
  </div>
</template>

<script lang="ts">
import {
  Component, Emit, Prop, Watch, Mixins,
} from 'vue-property-decorator';
import abpbase from 'geofly-framework-web-common/libs/abpbase';
import MapMixins from '@/map/mixins/mapMixins';

@Component({
  name: 'FileView',
  components: {},
})
export default class FileView extends Mixins(abpbase, MapMixins) {
  @Prop({ type: Object }) fileItem;

  state = {
    // 支持预览的文件列表
    fileType: ['txt', 'doc', 'docx', 'xls', 'xlsx', 'pdf', 'jpg', 'png', 'gif', 'bmp', 'jpeg'],
    // iframe需要通过office365预览的文件类型
    officeType: ['doc', 'docx', 'xls', 'xlsx'],
  }

  showIframe() {
    const { type } = this.fileItem;
    return this.state.fileType.includes(type);
  }

  /**
   * 处理文件预览路径
   * iframe中预览只能直接打开pdf文件,其他文件需要通过office365预览
   */
  getViewFilePath({ type, filePath }) {
    if (this.state.officeType.includes(type)) {
      return `https://view.officeapps.live.com/op/view.aspx?src=${filePath}`;
    }
    return filePath;
  }
}
</script>

<style lang="less" scoped>
.file-view {
  width: 100%;
  height: calc(100vh - 100px);
  border: 1px solid #ccc;
  .previewIframe {
    width: 100%;
    height: 100%;
    border: none;
  }
}
</style>
相关推荐
FYKJ_20101 小时前
springboot鲜花销售系统91056-计算机课程设计、毕业设计
vue.js·spring boot·后端·python·mysql·django·课程设计
Wx-bishekaifayuan2 小时前
Springcloud中学在线学习平台58883-计算机课程设计、毕业设计
java·vue.js·spring boot·学习·spring·spring cloud·课程设计
ljt27249606612 小时前
Vue笔记(九)--defineEmits
javascript·vue.js·笔记
2501_930707782 小时前
使用C#代码更改或删除 PDF 中的超链接
pdf
凤山老林17 小时前
Spring Boot 集成 iText 7 实现动态 PDF 生成与电子签章:合同、报表场景实战
spring boot·后端·pdf·itext7·电子签章
泡海椒17 小时前
趋势分析报表:jquick-pdf折线图PDF生成实战
java·大数据·运维·服务器·前端·pdf
qwfy17 小时前
我要用 AI-native 架构,从零重建一套制造业 ERP/MES
vue.js·spring boot·架构
dsyyyyy110118 小时前
Vue 3 Watch 监视完全指南
前端·javascript·vue.js
瞬时出道19 小时前
AI 回答为什么能一个字一个字蹦出来?前端搞懂 SSE 这一篇就够了
vue.js
泡海椒20 小时前
金融报表开发:jquick-pdf K 线图 PDF 可视化解决方案
java·开发语言·金融·pdf