vue实现文件预览

1、可以预览图片、文档、视频等

2、(注意)视频需要安装视频预览插件:cnpm install --save video.js

3、组件代码

<template>
  <div>
    <!--图片-->
    <div v-if="imgShow">
      <img :src="downloadUrl" />
    </div>
    <!--doc,excel-->
    <div v-else-if="docShow || excelShow">
      <iframe
        class="child"
        frameborder="0"
        :src="'https://view.officeapps.live.com/op/view.aspx?src=' + this.downloadUrl"
        :style="{ width: width, height: height }"
      >
      </iframe>
    </div>
    <!--视频-->
    <div v-else-if="videoShow">
      <video preload="auto" :height="height" :width="width" align="center" controls="true">
        <source :src="downloadUrl" type="video/mp4" />
      </video>
    </div>
    <!--其他不能预览的-->
    <div v-else-if="otherShow"></div>
  </div>
</template>
<script>
import Video from 'video.js';
import 'video.js/dist/video-js.css';
export default {
  props: { file: {} },
  data() {
    return {
      fileName: '',
      downloadUrl: '',
      imgShow: false,
      docShow: false,
      excelShow: false,
      videoShow: false,
      //其他不能预览的
      otherShow: false,
      height: window.innerHeight + 'px',
      width: '100%'
    };
  },
  created() {
    this.fileName = this.file.fileName;// 文件名称(包括后缀名)
    this.downloadUrl = this.file.downloadUrl;// 文件路径
    //.png, .jpeg, .jpg,
    // application/pdf, .txt, .xls, .xlsx, .doc, .docx,.ppt, .pptx,
    //.wps, .rtf,.pps, .ppsx,.mp4,.mp3,
    //.zip
    if (this.fileName.endsWith('png') || this.fileName.endsWith('jpg') || this.fileName.endsWith('jpeg')) {
      //图片
      this.imgShow = true;
    } else if (
      this.fileName.endsWith('docx') ||
      this.fileName.endsWith('doc') ||
      this.fileName.endsWith('pdf') ||
      this.fileName.endsWith('pptx') ||
      this.fileName.endsWith('ppt') ||
      this.fileName.endsWith('txt')
    ) {
      //doc
      this.docShow = true;
    } else if (this.fileName.endsWith('xlsx') || this.fileName.endsWith('xls')) {
      //excel
      this.excelShow = true;
    } else if (this.fileName.endsWith('mp4') || this.fileName.endsWith('mp3')) {
      this.videoShow = true;
    } else {
      this.otherShow = true;
    }
  }
};
</script>
<style scoped>
.child {
  width: 100%;
  height: 100%;
  scrolling: no;
  frameborder: 0;
  border: 0;
  marginwidth: 0;
  marginheight: 0;
}
</style>

4、引用并使用

<!-- 引用组件 -->
<el-dialog title="预览" 
	:visible.sync="previewFileShow" 
	idth="80%" 
	:modal-append-to-body='false'
    :close-on-press-escape='false'
    :close-on-click-modal='false'
    :before-close="handleClose" 
	style="height:auto">
    <PreviewFile :file="previewFile" v-if="previewFileShow"></PreviewFile>
</el-dialog>
<!-- data声明组件 -->
data(){
       return{
           previewFile:{}, // 传递的参数 
           previewFileShow:false // 默认预览框是关闭状态
       }
    },
<!-- 关闭组件-->
handleClose(){
	this.previewFileShow = true 
}
<!-- 点击预览触发的事件-->
preview(index,row){
       this.previewFileShow = true 
       this.previewFile.fileName = row.fileName
       this.previewFile.downloadUrl = row.url 
  }   

原文链接:https://blog.csdn.net/w_t_y_y/article/details/115767747

  • 11
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值