55

So I have been using Angular and Typescript for a long time. I cannot seem to find out what the Type for an Input file is.

For example:

<input type="file" (change)="fileChangeEvent($event)">

fileChangeEvent(e:?????){}

All examples purely just uses event without any type, and I am curious to know if such a type even exists.

0

7 Answers 7

64

For Angular 10 strict-type mode, we need to mention type in parameters. You can follow this way to achieve file-list from file input event

// HTML
<input type="file" (change)="uploadFile($event)">

// TS
uploadFile(event: Event) {
    const element = event.currentTarget as HTMLInputElement;
    let fileList: FileList | null = element.files;
    if (fileList) {
      console.log("FileUpload -> files", fileList);
    }
}

Hope this helps. Please share your feedback.

Sign up to request clarification or add additional context in comments.

4 Comments

Be careful with event.currentTarget. It contains an element to which the attached is handler but not an element which triggered the event.
As the above commenter said, should be event.target See Comparison of Event Targets.
how do you avoid the unresponsive change event if you select the same file twice?
Even better than a type cast is an instance of check: uploadFile(event: Event) { if (!(event.target instanceof HTMLInputElement)) { console.error("Unexpected event", event); return; } const element: HTMLInputElement = event.target; ... } .
36

Angular 10 and above:

component.html

    <input type="file" (change)="upload($event)">

component.ts

  upload(event: Event) {
      const target = event.target as HTMLInputElement;
      const files = target.files as FileList;
      console.log(files);
  }

Comments

23

Use File

The File interface provides information about files and allows JavaScript in a web page to access their content.

Try this

component.html

<input type="file" (change)="fileChangeEvent($event.target.files)">

component.ts

fileChangeEvent(e: File[]){
    fileName = e[0];
    fileType = fileName.type;
}

7 Comments

That could work since you pass in files with is Type File. But still begs the question what type is $event. Just weird that there is no clear answer.
It works, but it needs and adjustment, instead of fileName = e.files[0]; it must be const fileName = e[0].name; thank you.
Also, it does not change a lot, but e won't be an array of File, but a FileList. The most noticeable difference is that FileList does not implement the Iterator protocol, so for of loop will not work. developer.mozilla.org/en-US/docs/Web/API/FileList
We used to do that (with type FileList | readonly File[]), but if we activate Ivy's strictTemplates options, it errors on these lines: TS2339: Property 'files' does not exist on type 'EventTarget' and TS2531: Object is possibly 'null'. So either we use the ugly $any($event.target).files, or we are back to type $event.
IF you try $event.target?.files is it working?
|
2

If the type does not exist, manually build what you expect

<input type="file" (change)="fileChangeEvent($event)">

fileChangeEvent(e: Event & {target: HTMLInputElement & { files: FileList | null}}){}

Comments

0

In angular 18

<input type="file" class="hidden" accept=".xlsx" #upload (click)="uploadFile($event)" />
uploadFile(event: Event): void {
  const element = event.currentTarget as HTMLInputElement;
  const file = element?.files?.[0];
  if (file) {
    ...
  }
}

Comments

-1

The type is Event

fileChangeEvent(e: Event){}

2 Comments

Problem with using Event is. if you type e.target it only has 3 functions on that Type. AddEventListener, DispatchEvent, RemoveEventListener. So technically the type is not correct.
Depends on what you import I guess, VSC auto-suggested the right "Event" type right away in my case
-6

One line statement to get the file Type:

fileInput: any
fetchFileType($event);

fetchFileType(fileInput: any) {
  fileName = this.fileInput.target.files[0];
  fileType = fileName.type;
}

1 Comment

any should really not be used if absolutely necessary. Plus That is exactly what the question was about. The Type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.