美文网首页
vant上传音视频附件并且回显功能

vant上传音视频附件并且回显功能

作者: jesse28 | 来源:发表于2023-03-29 10:15 被阅读0次

fileUploader.vue组件


<template>
  <div>
    <van-uploader accept="avi , mp3,flv , mpg , mpeg , mpe , m1v , m2v , mpv2 , mp2v , dat , ts , tp , tpr , pva , pss , mp4 , m4v , m4p , m4b , 3gp , 3gpp , 3g2 , 3gp2 , ogg , mov , qt , amr , rm , ram , rmvb , rpm
" :after-read="afterRead">
      <van-button icon="plus" type="info" size="small">上 传</van-button>
    </van-uploader>
    <div class="container">
      <div v-for="(item, index) in fileList" :key="index" style="margin-right:30px;margin-top:20px">
        <img @click="handledetail(item.url)" style="width:60px;height:60px" v-if="['jpg','png','jpeg'].includes(item.fileType)" :src="item.url" />
        <img style="width:70px;height:70px" v-else-if="['mp3','aac'].includes(item.fileType)" src="@/assets/img/audio1.png" />
        <img style="width:60px;height:60px" v-else-if="['mp4','ogg'].includes(item.fileType)" src="@/assets/img/video1.png"/>
         <!-- <audio width="20" height="20" controls v-else-if="['mp3','aac'].includes(item.fileType)" :src="item.url"></audio> -->
        <!-- <video width="70" height="70" controls v-else-if="['mp4','ogg'].includes(item.fileType)" :src="item.url"></video>  -->
        <img style="width:60px;height:60px" v-else src="@/assets/img/file.png" />
        <van-icon name="delete" @click="beforeDeleteBack(index)" class="delte" />
      </div>
    </div>
  </div>
</template>

<script>
import { upLoadApi } from '@/api/oss.js'
import { ImagePreview, Dialog } from 'vant'
export default {
  data() {
    return {
      fileList: []
    }
  },
  props: {
    maxCount: {
      // 图片张数
      type: Number,
      default: 12
    },
    fileListArr: []
  },
  watch: {
    fileListArr(newValue, oldValue) {
      this.fileList = newValue
      console.log('音频我监听到的',this.fileList)
    }
  },
  methods: {
    // 图片查看功能
    handledetail(item) {
      console.log(item)
      ImagePreview({
        images: [item],
        closeable: true
      })
    },
    // 上传图片校验
    beforeRead(file) {
      if (
        file.type === 'image/jpeg' ||
        file.type === 'image/png' ||
        file.type === 'image/jpg' ||
        file.type === 'image/bpm' ||
        file.type === 'image/HEIC'
      ) {
        return true
      }
      this.$toast.fail('请上传正确格式的图片!')
      return false
    },
    // 照片文件删除
    beforeDeleteBack(index) {
      return new Promise((resolve, reject) => {
        this.$dialog
          .confirm({
            message: '确定删除?'
          })
          .then(() => {
            this.fileList.splice(index, 1)
            this.$toast.success('删除成功')
            console.log('文件删除之后的fileList', this.fileList)
            this.$emit('handlePic', this.fileList)
            resolve()
          })
          .catch(error => {
            this.$toast.success('已取消')
            reject(error)
          })
      })
    },
    // 上传图片
    afterRead(file) {
      console.log('上传的文件', file)
      let fd = new FormData()
      fd.append('webFile', file.file)
      upLoadApi(fd)
        .then(res => {
          if (res.code == 200) {
            let url = res.data.downPath.replace('~/', '')
            // this.fileList.pop()
            this.fileList.push({
              url: `${window.webConfig.api}/oss-api-service/${url}`,
              id: res.data.id,
              fileType: res.data.extension
            })
          }
          console.log('打印fileList', this.fileList)
          this.$emit('handlePic', this.fileList)
        })
        .catch(err => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  display: flex;
  flex-wrap: wrap;
}
audio{
  width:50px !important;
  height:50px !important;
}
</style>

父组件

    <van-field required name="pictureAfter" label="处置后照片" :rules="[{ required: true, message: '请选择' }]">
          <template #input>
            <uploader @handlePic="handlePicAfter" :fileListArr='fileList2'></uploader>
          </template>
        </van-field>
        <!-- 附件 -->
        <van-field name="pictureBefroe" label="附件">
          <template #input>
            <file-uploader @handlePic="handleFj" :fileListArr='fileList3'></file-uploader>
          </template>
        </van-field>
        <!-- 录音 -->
        <van-field name="pictureBefroe" label="录音">
          <template #input>
            <file-uploader @handlePic="handleVideo" :fileListArr='fileList4'></file-uploader>
          </template>
        </van-field>

          // 附件
            if (this.form.fj) {
              let temp = []
              this.form.fj.split(',').forEach(item => {
                getAttachmentApi(item).then(res => {
                  temp.push({
                    id: this.form.fj,
                    url: `${window.webConfig.api}/oss-api-service/` + res.data.downPath.replace('~/', ''),
                    fileType: res.data.extension
                  })
                })
              })
              this.fileList3 = temp
              console.log('输出附件', this.fileList3)
            }
            // 录音
            if (this.form.video) {
              let temp = []
              this.form.video.split(',').forEach(item => {
                getAttachmentApi(item).then(res => {
                  temp.push({
                    id: this.form.video,
                    url: `${window.webConfig.api}/oss-api-service/` + res.data.downPath.replace('~/', ''),
                    fileType: res.data.extension
                  })
                })
              })
              this.fileList4 = temp
              console.log('输出附件', this.fileList4)
            } //if-end
image.png

相关文章

  • 上传附件

    使用vant实现上传附件功能1.选择附件 2.上传附件到附件服务器 3.获取附件信息 上传附件一共需要三个步骤,首...

  • Ie9 上传图片回显功能

    图片上传回显功能,请参考 ie9 上传图片回显

  • Ext.NET 动态修改store参数失败, store和Gir

    概述 完整功能: 引入附件js文件后, 可以实现完整的附件功能. 这样每个需要附件上传区域的功能界面都可以传入一...

  • 图片在浏览器的缓存更新问题

    功能:图片上传功能,后端返回图片的url,前端回显。后端处理逻辑:根据图片上传的编号或者名字来确定上传oss的路径...

  • vue中实现附件上传

    前言 本篇主要记录在 Vue项目中 实现附件上传功能,可实现单/多附件上传,识别文件格式并用不同图标展示功能,及控...

  • Discuz! 群组附件只允许上传jpg/gif的解决方法

    今天在群组测试附件上传功能时,发现群组的上传附件允许的附件类型只有“jpg和gif”,在后台的用户组权限折腾了半天...

  • react-antd项目中多文件上传限制

    需求:一个附件列表,带上传功能,在上传时要求总附件数量不超过3个分析:在上传时进行before验证,在onChan...

  • vant 上传语音视频

    https://blog.csdn.net/dujyong/article/details/105053873?u...

  • bootstrap fileinput otherActionB

    最近处理bootstrap fileinput附件上传涉及附件下载问题,经过各种查询整合之后实现功能,整理文章免...

  • HAP_附件管理

    附件集成演示功能⽬标:为学⽣⻚⾯中的每⼀个学⽣信息添加附件管理功能,要求可以学⽣维护多个附件,并且可以下载和删除。...

网友评论

      本文标题:vant上传音视频附件并且回显功能

      本文链接:https://www.haomeiwen.com/subject/knkfddtx.html