Angular 自定义验证在角度模式下不起作用

Angular 自定义验证在角度模式下不起作用,angular,angular-reactive-forms,angular-forms,Angular,Angular Reactive Forms,Angular Forms,我有一个带有自定义验证的Angular表单生成器,但在自定义验证中读取该文件后,我无法获取该文件的类型 以下是stackblitz: TS文件 function checkFileType( control: AbstractControl ): { [key: string]: any } | null { const files: File = control.value; const errors: string[] = []; if (files) { con

我有一个带有自定义验证的Angular表单生成器,但在自定义验证中读取该文件后,我无法获取该文件的类型

以下是stackblitz:

TS文件

function checkFileType(
  control: AbstractControl
): { [key: string]: any } | null {
  const files: File = control.value;
  const errors: string[] = [];

  if (files) {
    console.log(files);
    if (files.type === "txt") {
      errors.push(`${files[0].name} has an invalid type of unknown\n`);
    }
    console.log(files.type); //This is always null. How can I get file type

    return errors.length >= 1 ? { invalid_type: errors } : null;
  }

  return null;
}


  onSelection($event) {
    const fileReader = new FileReader();
      const file = $event.target.files[0];
      fileReader.readAsDataURL(file);
      fileReader.onload = () => {
        this.reactiveForm.patchValue({
          file: fileReader.result
        });
      };  
  }


问题是来自于家庭。它将其编码为一个base 64字符串,这意味着它没有可查看的属性
类型。事实上,它没有任何类型可以查看,因为它是
字符串
,而不是
文件
。如果删除此项,并将
文件
变量设置为表单,则可以获得所需的属性

function checkFileType(
  control: AbstractControl
): { [key: string]: any } | null {
  const file: File = control.value;
  const errors: string[] = [];

  if (file) {
    console.log(file);
    if (file.type === "txt") {
      errors.push(`${file.name} has an invalid type of unknown\n`);
    }
    console.log(file.type); //This is always null. How can I get file type

    return errors.length >= 1 ? { invalid_type: errors } : null;
  }

  return null;
}


  onSelection($event) {
    const fileReader = new FileReader();
      const file = $event.target.files[0];
      fileReader.onload = () => {
        this.reactiveForm.patchValue({
          file: file
        });
      };  
  }