vue中预览视频,word,pdf,xlsx

前提,下载依赖

#docx文档预览
npm install @vue-office/docx

#excel文档预览
npm install @vue-office/excel

#pdf文档预览
npm install @vue-office/pdf

如果vue版本是vue2.6版本或者以下版本还需要安装@vue/composition-api

npm install @vue/composition-api

 

1、预览word

<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :append-to-body="true"
    width="80%"
    fullscreen
    title="文件预览">
    <vue-office-docx
      :src="docxUrl"
      style="height: 100vh"
      @rendered="renderedHandler"
      @error="errorHandler"
    />
  </el-dialog>
</template>

<script>
import VueOfficeDocx from '@vue-office/docx'
export default {
  name: 'PdfView',
  components: {
    VueOfficeDocx
  },
  data() {
    return {
      docxUrl: '/pdf-test.docx',
      visible: false
    }
  },
  mounted() {},
  methods: {
    init(src) {
      this.docxUrl = src
      this.visible = true
    },
    renderedHandler() {
      console.log('渲染完成')
    },
    errorHandler() {
      console.log('渲染失败')
    }
  }
}
</script>
    <style scoped>
    </style>

2、预览pdf

<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :append-to-body="true"
    width="80%"
    fullscreen
    title="文件预览">
    <vue-office-pdf
      :src="pdfUrl"
      style="height: 100vh"
      @rendered="renderedHandler"
      @error="errorHandler"
    />
  </el-dialog>
</template>

<script>
import VueOfficePdf from '@vue-office/pdf'
export default {
  name: 'PdfView',
  components: {
    VueOfficePdf
  },
  data() {
    return {
      pdfUrl: '/pdf-test.pdf',
      visible: false
    }
  },
  mounted() {},
  methods: {
    init(src) {
      this.pdfUrl = src
      this.visible = true
    },
    renderedHandler() {
      console.log('渲染完成')
    },
    errorHandler() {
      console.log('渲染失败')
    }
  }
}
</script>
  <style scoped>
  </style>

3、excel

<template>
    <vue-office-excel
        :src="excel"
        style="height: 100vh;"
        @rendered="renderedHandler"
        @error="errorHandler"
    />
</template>

<script>
//引入VueOfficeExcel组件
import VueOfficeExcel from '@vue-office/excel'
//引入相关样式
import '@vue-office/excel/lib/index.css'

export default {
    components: {
        VueOfficeExcel
    },
    data() {
        return {
            excel: 'http://static.shanhuxueyuan.com/demo/excel.xlsx'//设置文档地址
        }
    },
    methods: {
        renderedHandler() {
            console.log("渲染完成")
        },
        errorHandler() {
            console.log("渲染失败")
        }
    }
}
</script>

4、视频

<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="visible"
    :append-to-body="true"
    width="60%"
    fullscreen
    title="视频播放">
    <div>
      <video :src="videoUrl" width="100%" height="100%" controls/>
      <!-- <el-button @click="playVideo">播放视频</el-button>
      <el-button @click="pauseVideo">暂停视频</el-button> -->
    </el-button></div>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      videoUrl: 'your-video-url.mp4', // 视频文件的URL
      videoPlayer: null,
      visible: false
    }
  },
  mounted() {
    this.videoPlayer = this.$el.querySelector('video')
  },
  methods: {
    init(src) {
      this.videoUrl = src
      this.visible = true
    },
    playVideo() {
      if (this.videoPlayer) {
        this.videoPlayer.play()
      }
    },
    pauseVideo() {
      if (this.videoPlayer) {
        this.videoPlayer.pause()
      }
    }
  }
}
</script>

注意:如果返回的不是文件地址,而是文件流

可以调用接口获取文件的ArrayBuffer数据,传递给src属性,业绩就是将文件流转化为ArrayBuffer数据

<template>
    <vue-office-docx
        :src="docx"
        style="height: 100vh;"
        @rendered="rendered"
    />
</template>

<script>
//引入VueOfficeDocx组件
import VueOfficeDocx from '@vue-office/docx'
//引入相关样式
import '@vue-office/docx/lib/index.css'

export default {
    components:{
        VueOfficeDocx
    },
    data(){
        return {
            docx: ''
        }
    },
    mounted(){
        fetch('你的API文件地址', {
            method: 'post'
        }).then(res=>{
            //读取文件的arrayBuffer
            res.arrayBuffer().then(res=>{
                this.docx = res
            })
        })
    },
    methods:{
        rendered(){
            console.log("渲染完成")
        }
    }
}
</script>

 

相关推荐

  1. vue视频,word,pdf,xlsx

    2024-07-23 05:16:01       16 阅读
  2. Vue+ElementUI实现文件照片音频视频

    2024-07-23 05:16:01       29 阅读
  3. vue pdf

    2024-07-23 05:16:01       14 阅读
  4. vue3 H5项目实现PDF

    2024-07-23 05:16:01       50 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 05:16:01       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 05:16:01       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 05:16:01       45 阅读
  4. Python语言-面向对象

    2024-07-23 05:16:01       55 阅读

热门阅读

  1. service

    service

    2024-07-23 05:16:01      14 阅读
  2. vue3 页面引入组件

    2024-07-23 05:16:01       17 阅读
  3. NotebookApp Error unpacking user from cookie

    2024-07-23 05:16:01       15 阅读
  4. PHP基础语法(三)

    2024-07-23 05:16:01       18 阅读
  5. Linux中利用消息队列给两个程序切换显示到前台

    2024-07-23 05:16:01       18 阅读
  6. Python面试题:Python中的类型提示与静态类型检查

    2024-07-23 05:16:01       17 阅读
  7. 【架构专题】微服务架构

    2024-07-23 05:16:01       13 阅读
  8. 学术研究期刊

    2024-07-23 05:16:01       13 阅读
  9. 设计模式实战:图形编辑器的设计与实现

    2024-07-23 05:16:01       16 阅读