vue上传文件获取音视频时长

    <el-upload
      action
      :http-request="Upload"
      :before-upload="beforeAvatarUpload"
      :on-preview="handlePreview"
      :before-remove="beforeRemove"
      :on-remove="handleRemove"
      :on-success="handleSuccess"
      :on-exceed="handleExceed"
      drag
      :limit="limit"
      :file-list="fileList"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">
        将文件拖到此处,或
        <em>点击上传</em>
      </div>
      <div slot="tip" class="el-upload__tip">上传文件大小不能超过 2G</div>
    </el-upload>
//文件上传前的校验
data(){
	return {
		audioDuration:''   //时长
	}
},
methods:{
	beforeAvatarUpload(file) {
		this.getTimes(file); //
	},
	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);
	    });
	},
}

你可能感兴趣的:(Vue.js)