通过before-upload限制文件方法来获取视频的时长
<el-upload
class="upload-demo"
action="#"
:http-request="handleFileUpload"
:before-upload="handleBeforeUpload">
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">
只能上传90s以内的视频
</div>
</el-upload>
handleBeforeUpload(file){
console.log(file);
const checkDuration = (file) => {
return new Promise((resolve, reject) => {
const videoUrl = URL.createObjectURL(file);
const video = document.createElement('video');
video.preload = 'metadata';
video.onloadedmetadata = () => {
const duration = video.duration;
URL.revokeObjectURL(videoUrl); // 释放URL对象
resolve(duration);
};
video.onerror = () => {
URL.revokeObjectURL(videoUrl); // 释放URL对象
reject(new Error('视频加载出错'));
};
video.src = videoUrl; // 设置视频源
});
};
return checkDuration(file).then(duration => {
if (duration > 90) {
this.$message.error('视频时长不能超过90秒!');
return false;
}
console.log(duration);
return true; // 时长检查通过,可以继续上传
}).catch(error => {
this.$message.error('获取视频时长失败:' + error.message);
return false; // 获取时长失败,阻止上传
});
},
handleFileUpload(){
}