elementUI i中的 el-upload上传音频文件获取音频时间长度

html代码:

<el-upload :action="addForm.path" v-model="addForm.path" class="avatar-uploader" :http-request="fileUploadHandler"  :multiple="false" :before-upload="beforeAvatarUpload">
            <el-button size="small" type="primary">点击上传</el-button>
</el-upload>

js代码:

// 处理上传音频
async fileUploadHandler(event) {
      var that = this
      this.uploadLoading = true
      const { file } = event
      const formData = new FormData()
      formData.append('file', file)
      formData.append('path', 'upload/audio')
      console.log(formData)
      try {
        let res = await axios.post(`/ui/core/file/upload`, formData, {
          headers: {
            'Content-Type': 'multipart/form-data'
          }
        })
        this.uploadLoading = false
        console.log(res)
        if (res.data.status === 1) {
          that.addForm.path = '存储路径' + file.name
        } else {
          this.$message.error('上传失败,请稍后再试下!')
        }
      } catch (e) {
        this.uploadLoading = false
      }
},
还可以获取音频的大小和时间长度

html代码:

<el-upload
             id="audioUpload"
              class="avatar-uploader"
              :action="upLoadAudio"
              :show-file-list="true"
              :multiple="false"
              :file-list="fileList"
              :on-remove="handleRemove"
              :limit="1"
              :auto-upload="true"
              :on-change="handleChangeAudio"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
          <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
          <div slot="tip" class="el-upload__tip" style="margin-bottom: 10px">
              只能上传mp3文件,且不超过2M,播放长度不超过60s
          </div>
</el-upload>

js:

beforeAvatarUpload(file) {
                // 文件类型进行判断
                const isAudio = file.type === "audio/mp3" || file.type === "audio/mpeg";
                // 限制上传文件大小 2M
                const isLt2M = file.size / 1024 / 1024 < 2;
                const isTime60S = this.audioDuration >= 60 ? true : '';
                // 获取时长
                this.getTimes(file);
                if (!isAudio) {
                    this.$message.error("上传文件只能是Mp3格式!");
                    this.fileList = [];
                } else {
                    if (!isLt2M) {
                        this.$message.error("上传文件大小不能超过 2MB!");
                        this.fileList = [];
                    } else {
                        if (!isTime60S) {
                            this.$message.error("上传文件时长不能超过60秒!");
                            this.fileList = [];
                        }
                    }
                }
                return isAudio && isLt2M && isTime60S
},
getTimes(file) {
                var content = file;
                //获取录音时长
                var url = URL.createObjectURL(content);
                //经测试,发现audio也可获取视频的时长
                var audioElement = new Audio(url);
                audioElement.addEventListener("loadedmetadata", (_event) => {
                    this.audioDuration = parseInt(audioElement.duration);
                    // console.log(this.audioDuration);
                });
 },
  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值