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>
相关推荐
普修罗双战士10 小时前
项目设计-文章系统发布文章完整前后端设计
java·数据库·vue.js·spring boot·git·intellij-idea
Csvn10 小时前
Vue 3 Composition API 深度解析
前端·vue.js
开开心心_Every10 小时前
免费简洁的安卓黄历日历,软件推荐
运维·服务器·随机森林·pdf·电脑·excel·最小二乘法
旷世奇才李先生10 小时前
Vue 3\+Vite\+Pinia实战:前端工程化与组件化开发全指南
前端·vue.js
周bro10 小时前
vue2+element ui 中的el-table表格 选中当前行当前行变色,单选/多选--------续集:表格样式修改整合
vue.js·ui·elementui
VBAMatrix1 天前
deepseek-v4正式接入Excel,一键生成财务分析报告
word·excel·审计·财务分析·deepseek·会计师事务所·tb工具箱
不会敲代码11 天前
从零到一:用 Vue3 + Kimi 大模型打造「拍照记单词」AI 应用
vue.js·typescript·aigc
隔壁的大叔1 天前
Markdown 渲染如何穿插自定义组件
前端·javascript·vue.js
RONIN1 天前
脚手架搭建项目框架(create-vite、vue-cli、create-vue、quasar-cli)
vue.js
愚者Pro1 天前
Flutter基础学习
前端·javascript·vue.js