Vue中使用js实现视频的预览,图片的预览,文字的预览

效果如图:

 

1.首先我们在模板里:
  <div v-if="textUrl" class="imgPreview">
    {{ textUrl }}
  </div>
  <div v-if="videoUrl" class="imgPreview">
    <video alt="" style="width:300px;height:300px" controls>
      <source :src="videoUrl" type="video/mp4">
      <source :src="videoUrl" type="video/ogg">
    </video>
  </div>
  <div  v-if="photoUrl"  class="imgPreview">
    <img :src="photoUrl" alt="" style="width:300px;height:300px">
  </div>
</div>

这里通过v-if来显示不同的效果:

2.方法如下:

getAccessData(absolutePath,index) {
  this.picListIndex = index
  this.currentPicPath = absolutePath //路径
  let picIndex = absolutePath.lastIndexOf('.');
  this.ext = absolutePath.substr(picIndex + 1);//文件类型
  let param = new FormData();
  param.append('file', absolutePath);
  if (this.ext == 'jpg' || this.ext == 'png' || this.ext == 'gif' || this.ext == 'jpeg') {
//对应自己的文件服务接口,看好参数
    getFilePhoto(this.$route.query.token,param).then(res => {  、、
      let _this = this
      let blob = new Blob([res.data])
      var reader = new window.FileReader();
      reader.readAsDataURL(blob);
      reader.onloadend = function (e) {
        _this.photoUrl = e.target.result
        _this.textUrl= '';
        _this.videoUrl= '';
        console.log(_this.photoUrl)
      }
    })
  }
  if (this.ext == 'mp4' || this.ext == 'avi' || this.ext == 'rmvb' || this.ext == 'rm' || this.ext == 'flv') {
//对应自己的文件服务接口,看好参数
    getFilePhoto(this.$route.query.token,param).then(res => { 
      let _this = this
      let blob = new Blob([res.data],{type:'video/mp4'})
      var reader = new window.FileReader();
      reader.readAsDataURL(blob);
      reader.onloadend = function (e) {
        _this.videoUrl = e.target.result
        _this.textUrl= '';
        _this.photoUrl= '';
      }
    })
  }
  if (this.ext == 'txt' ||this. ext == 'doc' || this.ext == 'docx' || this.ext == 'xls' || this.ext == 'xlsx' || this.ext == 'pdf') {
//对应自己的文件服务接口,看好参数
    getFilePhoto(this.$route.query.token,param).then(res => {
      let _this = this
      let blob = new Blob([res.data])
      var reader = new window.FileReader();
      reader.readAsText(blob,'utf-8');
      reader.onloadend = function (e) {
        _this.textUrl = e.target.result
        _this.photoUrl= '';
        _this.videoUrl= '';
        console.log(_this.textUrl)
      }
    })
  }
}

一些细节:data里边的初始定义,方便全局调用。

接口里边参数的格式:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值