【第014篇】基于vue+elementui设计的在线预览文件(可预览图片、音频、视频、PDF、WORD、EXCEL文档)

一、在线预览组件代码封装

1、文件目录截图

在这里插入图片描述

2、文件代码
(1)preview-file.vue 文件内容
<template>
  <!--
  1-先安装插件,执行命令:npm i --save video.js
  2-在main.js文件中引入:
  import 'video.js/dist/video-js.min.css'
  import previewFile from '@/components/preview-file'
  Vue.use(previewFile)
  使用:
  <preview-file :file="file" ref="previewFile"></preview-file>

  data() {
   
    return {
   
      file: {
   
        visible: false, // 是否显示弹出框
        title: '', // 文件名称(包含后缀名)
        url: '' // 文件路径
      }
    }
  },
  methods: {
   
    onPreview (file) {
    // 预览文件
      if (file.response !== null && file.response !== undefined && file.response !== '') {
   
        this.file.visible = true // 是否显示弹出框
        this.file.title = file.response.data.fileName // 文件名称(含后缀名)
        this.file.url = file.response.data.src // 文件地址
        this.$refs.previewFile.init()
      } else {
   
        this.$message.info('文件正在上传,等上传完成后再预览!')
      }
    }
  }
  -->
  <el-dialog :visible.sync="this.file.visible" :title="`${this.file.title}`" :before-close="handleClose" :width="this.width" :close-on-click-modal="false" :close-on-press-escape="false" top="0" append-to-body>
    <div v-if="this.isImage" style="text-align: center;">
      <img :src="this.file.url" width="100%" alt />
    </div>
    <div v-else-if="this.isPdf">
      <iframe :src="this.file.url" width="100%" :height="this.iframeH" frameborder="0"/>
    </div>
    <div v-else-if="this.isAudio">
      <!-- autoplay="autoplay" 支持自动播放音频文件 -->
      <template>
        <audio :src="this.file.url" controls="controls" ref="audio" style="width: 100%;">
          您的浏览器不支持音频元素。
        </audio>
        <div style="width: 100%;text-align: center;margin-top: 20px;margin-bottom: 20px;">
          <el-button width="18rem" size="small" plain @click="playAudio">播放</el-button>
          <el-button width="18rem" size="small" plain @click="pauseAudio">暂停</el-button>
          <el-button width="18rem" size="small" plain @click="stopAudio">停止</el-button>
        </div>
      </template>
    </div>
    <div class="videoArea" v-else-if="this.isVideo">
      <video ref="videoPlayer" controls :src="this.file.url" @timeupdate="updateTime" :class="['movie-show-video']" style="width: 100%; height: 100%; object-fit: fill"></video>
      <div style="width: 100%;text-align: center;margin-top: 20px;margin-bottom: 20px;">
        <el-button width="18rem" size="small" plain @click="playVideo">播放</el-button>
        <el-button width="18rem" size="small" plain @click="pauseVideo">暂停</el-button>
        <el-button width="18rem" size="small" plain @click="stopVideo">停止</el-button>
        <el-button width="18rem" size="small" plain @click="forwardVideo">前进</el-button>
        <el-button width="18rem" size="small" plain @click="backVideo">倒退</el-button>
      </div>
    </div>
    <div v-else-if="this.isOffice">
      <iframe :src="`https://view.officeapps.live.com/op/view.aspx?src=${this.file.url}`" width="100%" :height="this.iframeH" frameborder="0"/>
    </div>
    <div v-else style="text-align: center;">
      暂不支持在线预览
    </div>
  </el-dialog>
</template>
<script>
export default {
   
  name: 'PreviewFile',
  props: {
   
    file: {
   
      type: Object,
      default: function () {
   
        return {
   
          visible: false, // 是否显示弹出框
          title: '', // 文件名称(含后缀名)
          url: '' // 文件路径
        }
      }
    }
  },
  data () {
   
    return {
   
      isImage: false, // 是否为图片类型,如:jpg/jpeg/png
      isPdf: false, // 是否为pdf文件
      isAudio: false, // 是否为音频文件,如:mp3/m4a/flac
      isVideo: false, // 是否为视频文件,如:mp4/avi/mov
      isOffice
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
您可以使用 `xlsx` 库和 `FileSaver.js` 库来实现VueElement UI导出Excel文件的功能。以下是一个简单的示例: 1. 首先,安装 `xlsx` 和 `file-saver` 包: ```bash npm install xlsx file-saver ``` 2. 在 Vue 组件中引入所需的库: ```javascript import XLSX from 'xlsx'; import { saveAs } from 'file-saver'; ``` 3. 创建一个方法来导出Excel文件: ```javascript methods: { exportToExcel() { // 创建工作簿和工作表 const workbook = XLSX.utils.book_new(); const worksheet = XLSX.utils.json_to_sheet(this.data); // 将工作表添加到工作簿 XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1'); // 生成Excel文件的二进制数据 const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' }); // 将二进制数据转换为Blob对象 const blob = new Blob([excelBuffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' }); // 使用FileSaver.js保存文件 saveAs(blob, 'data.xlsx'); } } ``` 在上面的代码中,`this.data` 是要导出的数据,可以是一个数组或对象。 4. 在模板中添加一个按钮来触发导出操作: ```html <template> <div> <button @click="exportToExcel">导出Excel</button> </div> </template> ``` 这样,当用户点击“导出Excel”按钮时,将会触发 `exportToExcel` 方法,并导出Excel文件。 请注意,为了使上述代码正常工作,您需要在项目中正确引入 `xlsx` 和 `file-saver` 库,并将相关的样式和依赖项加载到您的项目中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

嘉&年华

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

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

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

打赏作者

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

抵扣说明:

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

余额充值