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);
                });
 },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM