Table Of Contents

骑驴找蚂蚁

全干工程师

Property 'files' does not exist on type 'EventTarget' error in typescript

interface HTMLInputEvent extends Event {
    target: HTMLInputElement & EventTarget;
}

document.getElementById("customimage").onchange = function(e?: HTMLInputEvent) {
    let files: any = e.target.files[0]; 
    //...
}

fileChange($event: Event) {
      const files = (<HTMLInputElement>$event.target).files;
      if (files.length === 0) {
        return ;
      }
      this.progress = true;
      this.uploadService.uploadFile(files[0])
        .subscribe(event => {
          if (event.type === HttpEventType.UploadProgress) {
            this.progressValue = Math.round(100 * event.loaded / event.total);
            console.log(`File is ${this.progressValue}% loaded.`);
          } else if (event instanceof HttpResponse) {
            console.log('File is completely loaded!');
            this.progressValue = 100;
            of(null).pipe(delay(1500)).subscribe(() => { this.progress = false; });
          }
        });
  }

留言