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

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

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

效果:
在这里插入图片描述在这里插入图片描述
在这里插入图片描述

组件代码:

<!--
 * @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>

要在Vue中实现txtpdfword文件预览,可以使用不同的技术来处理不同类型的文件。 对于txt文件,可以直接将其内容显示在页面上。可以通过以下步骤来实现: 1. 在Vue组件中加入一个文件选择的input元素: ```html <input type="file" ref="fileInput" @change="previewFile"> ``` 2. 在Vue组件的methods中定义处理文件选择的方法: ```javascript methods: { previewFile(event) { const file = event.target.files[0]; const reader = new FileReader(); // 读取文件内容 reader.onload = (e) => { this.fileContent = e.target.result; }; // 开始读取文件 reader.readAsText(file); } } ``` 3. 在Vue组件的data中定义一个变量`fileContent`,用来存储文件内容: ```javascript data() { return { fileContent: '' }; } ``` 4. 在Vue组件的模板中展示文件内容: ```html <div v-if="fileContent"> <pre>{{ fileContent }}</pre> </div> ``` 对于pdfword文件,可以使用第三方库来实现文件预览。常用的库包括`pdf.js`和`mammoth.js`。以下是使用这两个库的示例代码: 1. 安装pdf.js和mammoth.js库: ```bash npm install pdfjs-dist mammoth ``` 2. 在Vue组件中引入pdf.js和mammoth.js库: ```javascript import pdfjsLib from 'pdfjs-dist'; import * as mammoth from 'mammoth'; ``` 3. 在Vue组件的methods中定义处理文件选择的方法: ```javascript methods: { previewFile(event) { const file = event.target.files[0]; if (file.type === 'application/pdf') { this.previewPDF(file); } else if (file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') { this.previewWord(file); } }, previewPDF(file) { const reader = new FileReader(); reader.onload = (e) => { const pdfData = new Uint8Array(e.target.result); pdfjsLib.getDocument({ data: pdfData }).promise.then((pdf) => { pdf.getPage(1).then((page) => { const viewport = page.getViewport({ scale: 1 }); const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); canvas.width = viewport.width; canvas.height = viewport.height; page.render({ canvasContext: context, viewport }).promise.then(() => { this.fileContent = canvas.toDataURL(); }); }); }); }; reader.readAsArrayBuffer(file); }, previewWord(file) { const reader = new FileReader(); reader.onload = (e) => { const arrayBuffer = e.target.result; mammoth.extractRawText({ arrayBuffer: arrayBuffer }).then((result) => { this.fileContent = result.value; }); }; reader.readAsArrayBuffer(file); } } ``` 4. 在Vue组件的data中定义一个变量`fileContent`,用来存储文件内容: ```javascript data() { return { fileContent: '' }; } ``` 5. 在Vue组件的模板中展示文件内容: ```html <div v-if="fileContent"> <pre>{{ fileContent }}</pre> </div> ``` 通过以上步骤,你可以在Vue中实现txtpdfword文件预览功能。根据文件类型的不同,选择不同的处理方法来显示文件内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

星月前端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值