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>
相关推荐
伟大的兔神8 小时前
我做了一个本地优先的 AI 图片工作台:Loomora v1.0.0 正式发布
前端·javascript·vue.js
Flynt11 小时前
我扒了Fantastic-admin 6.0的源码,聊聊"AI时代版本答案"这个称号
vue.js·ai编程·cursor
码艺-Alimjan12 小时前
Vue项目源码备份最佳实践:无视node_modules,打包体积仅2MB
前端·javascript·vue.js
风月说与山鬼14 小时前
十二、Vue插件
前端·vue.js
reasonsummer16 小时前
【办公类-109-12】20260901圆形挂牌(模版重制:文本框圆形重叠_word编辑单面_接送卡&被子卡&床卡&入园卡)
python·word
AI导出鸭20 小时前
怎么让Claude做表格?AI导出鸭苹果版将Claude输出的Markdown表格或结构化列表智能解析为二维数据,一键导出Excel/Word标准表格。
人工智能·chatgpt·word·excel·ai导出鸭
风月说与山鬼20 小时前
四、浏览器存储
前端·vue.js
阿懂在掘金20 小时前
记录四年前的一次前端项目权限方案改造
前端·vue.js·架构
禁止摆烂_才浅20 小时前
Vue2 高频面试题
前端·vue.js·面试
禁止摆烂_才浅20 小时前
Vue3 高频面试题
前端·vue.js·面试