1.封装获取时间方法。
getTimes(file) {
var content = file;
//获取录音时长
var url = URL.createObjectURL(content);
//经测试,发现audio也可获取视频的时长
var audioElement = new Audio(url);
audioElement.addEventListener("loadedmetadata", (_event) => {
//保存获取时间长度
this.dataFile.videoTime = parseInt(audioElement.duration)
});
},
2.再upload上传之前拿到上传的file文件,调用方法。
//上传之前的操作
// file.type.includes 文件类型判断
beforeAvatarUpload(file) {
if (file.type.includes('image')) {
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
this.$message.error('上传图片大小不能超过 2MB!');
this.isok = false
} else {
this.isok = true
this.updataimg.fileKey = this.utypeTexe
}
}
if (file.type.includes('video')) {
this.getTimes(file)
const isLt2M = file.size / 1024 / 1024 < 500;
if (!isLt2M) {
this.$message.error('上传视频大小不能超过 500MB!');
this.isok = false
} else {
this.isok = true
this.updataimg.fileKey = this.utypeTexe
}
}
if (file.type.includes('audio')) {
this.getTimes(file)
const isLt2M = file.size / 1024 / 1024 < 500;
if (!isLt2M) {
this.$message.error('上传音频大小不能超过 500MB!');
this.isok = false
} else {
this.isok = true
this.updataimg.fileKey = this.utypeTexe
}
}
},