Angular 角度视频验证器don';等不及了
我的视频验证器有问题。我并不总是工作。在测试之前,它有时会做出“无效”的响应 以下是文件拾取事件:Angular 角度视频验证器don';等不及了,angular,Angular,我的视频验证器有问题。我并不总是工作。在测试之前,它有时会做出“无效”的响应 以下是文件拾取事件: onVideoPick(event: Event) { const file = (event.target as HTMLInputElement).files[0]; console.log('Video picked'); this.CreaLotForm.get('video').patchValue(file); this.CreaLotForm.get(
onVideoPick(event: Event) {
const file = (event.target as HTMLInputElement).files[0];
console.log('Video picked');
this.CreaLotForm.get('video').patchValue(file);
this.CreaLotForm.get('video').updateValueAndValidity();
const reader = new FileReader();
var count = 0
let IsValid = this.CreaLotForm.get('video').valid;
reader.onload = () => {
while (this.CreaLotForm.get('video').status === 'PENDING') {
console.log((this.CreaLotForm.get('video').status));
this.errorMessage = "Checking...";
count = count + 1;
if (count === 100) { break };
}
if (this.CreaLotForm.get('video').valid) {
this.videoPreview = this.sanitizer.bypassSecurityTrustUrl(URL.createObjectURL(file));
this.errorMessage = "this video is OK";
console.log('VID valid');
} else {
this.errorMessage = "Error upload video, please retry (Type .mp4 only)";
console.log('VID validnot');
}
};
reader.readAsDataURL(file);
}
这是验证程序代码
export const mimeTypeVid = (
control: AbstractControl
): Promise<{ [key: string]: any }> | Observable<{ [key: string]: any }> => {
if ((typeof control.value) === 'string') {
return of(null);
}
const file = control.value as File;
const fileReader = new FileReader();
const frObs = Observable.create(
(observer: Observer<{ [key: string]: any }>) => {
fileReader.addEventListener('loadend', () => {
let isValid = false;
console.log('file type '+file.type);
switch (file.type) {
case 'video/mp4':
isValid = true;
break;
case 'video/mp4v-es':
isValid = true;
break;
default:
isValid = false;
break;
}
console.log("Mime Vid Validators"+ isValid)
if (isValid) {
observer.next(null);
} else {
observer.next({ invalidMimeType: true });
}
observer.complete();
});
fileReader.readAsArrayBuffer(file);
}
);
return frObs;
};
export const mimeTypeVid=(
控件:抽象控件
):承诺|可观察=>{
if((typeof control.value)==“string”){
返回(空);
}
const file=control.value作为文件;
const fileReader=new fileReader();
const frObs=Observable.create(
(观察员:观察员)=>{
fileReader.addEventListener('loadend',()=>{
设isValid=false;
console.log('file type'+file.type);
开关(file.type){
案例“视频/mp4”:
isValid=true;
打破
案例“视频/mp4v es”:
isValid=true;
打破
违约:
isValid=false;
打破
}
log(“Mime视频验证程序”+isValid)
如果(有效){
observer.next(空);
}否则{
下一步({invalidMimeType:true});
}
observer.complete();
});
readAsArrayBuffer(文件);
}
);
返回蛙泳;
};
这在80%的情况下有效,但有时您必须使用另一个文件更新文件拾取以使其有效
因为它在浏览器控制台中响应:
维德瓦利德诺
文件类型视频/mp4
Mime视频校验器
最好的选择是什么?设置计时器?更改验证器
谢谢你的帮助:)