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>
相关推荐
Wang's Blog2 小时前
前端FAQ: 描述⼀下你最近使⽤过的前端框架,并解释为何选择它们?
前端·vue.js·faq
callmeSoon2 小时前
Solid 初探:启发 Vue Vapor 的极致框架
vue.js·前端框架·响应式设计
小二·2 小时前
从零到上线:Spring Boot 3 + Spring Cloud Alibaba + Vue 3 构建高可用 RBAC 微服务系统(超详细实战)
vue.js·spring boot·微服务
xiaohe06013 小时前
🥳 Uni ECharts 2.1 发布:正式支持鸿蒙,零成本迁移、全平台兼容、跨端开发零负担!
vue.js·uni-app·echarts
RAY_CHEN.3 小时前
vue递归组件-笔记
前端·javascript·vue.js
『 时光荏苒 』4 小时前
网页变成PDF下载到本地
前端·javascript·pdf·网页下载成
chenchihwen5 小时前
AI代码开发宝库系列:PDF文档解析MinerU
人工智能·python·pdf·dashscope
毕设十刻15 小时前
基于Vue的学分预警系统98k51(程序 + 源码 + 数据库 + 调试部署 + 开发环境配置),配套论文文档字数达万字以上,文末可获取,系统界面展示置于文末
前端·数据库·vue.js
Source.Liu15 小时前
【PDF-rs】pdf子项目的lib.rs文件
pdf
学会用脚编程16 小时前
word转Pdf,在window正常,放在linux服务器上就转出来中文是空白
pdf·word