1.饿了么的上传组件 <el-upload>,监听他的变化 on-change
<el-upload
:on-change="uploadVideoChange"
>
<el-button size="small" type="primary">
<i class="el-icon-upload" />点击上传
</el-button>
</el-upload>
我看很多其他文章都是在 beforeUpload那里监听,我这里不好用,就用在了on change上
2.可以拿到上传的file对象
!!注意 : 这里的file对象是elemenui自己的对象,我们要把对象中的raw当做原生的对象传到方法中去
uploadVideoChange(file) {
this.getVideoMsg(file.raw).then((videoinfo) => {
const { duration, height, width } = videoinfo;
//获取到视频的时长,高度,宽度
});
//其他操作
},
getVideoMsg(file) {
return new Promise((resolve) => {
let videoElement = document.createElement("video");
videoElement.src = URL.createObjectURL(file);
videoElement.addEventListener("loadedmetadata", function () {
resolve({
duration: videoElement.duration,
height: videoElement.videoHeight,
width: videoElement.videoWidth,
});
});
});
},
将file.raw传到 getVideoMsg方法中去,获取到我们需要的信息,存为promise对象返回去,在onchage监听的事件中,就可以根据返回的信息进行操作
比如宽度需要大于多少,时长不能大于多少,满足条件再进行上传,等等~