0

I have this:

<input type="file" class="d-none" (change)="onPhotoSelected($event)" #photoInput>

and on my component.ts:

  onPhotoSelected(event: HtmlInputEvent): void {
    if (event.target.files && event.target.files[0]) {
      this.file = <File>event.target.files[0];
      // image preview
      const reader = new FileReader();
      reader.onload = e => this.photoSelected = reader.result;
      reader.readAsDataURL(this.file);
    }
  }

but I'm getting this issue:

Error: src/app/components/photo-form/photo-form.component.html:15:77 - error TS2345: Argument of type 'Event' is not assignable to parameter of type 'HtmlInputEvent'.

why??

I'm working with angular with typescript

  • 1
    Does this answer your question? [What Typescript type is a change event? (In Angular)](https://stackoverflow.com/questions/57700163/what-typescript-type-is-a-change-event-in-angular) – Juan May 31 '23 at 14:06
  • yes, I was testing, but I'm getting ```Object is possibly null``` – Jhon Estrada May 31 '23 at 14:11
  • 1
    @JhonEstrada for the second issue, check this https://stackoverflow.com/questions/61573872/typescript-object-is-possibly-null-when-getting-files-from-event – callback May 31 '23 at 14:16
  • the event argument on method onPhotoSelected should be of type Event. And then in the method you can cast it as HtmlInputEvent – Bruno Miguel May 31 '23 at 14:22
  • I added ```file: File | null;``` and ```if (!event.target.files) return;``` but I'm getting ```event.target is possibly null``` yet..... I'm inquiring why – Jhon Estrada May 31 '23 at 14:36

0 Answers0