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

AngularTypescriptIonic2Exif Js

Angular Problem Overview


I am trying to access the value of the input file from my ionic 2 application but still I'm facing the issue of property files does not exist on type 'EventTarget'. As it is properly working in js but not in typescript. The code is given below:

  document.getElementById("customimage").onchange= function(e?) {
            var files: any = e.target.files[0]; 
              EXIF.getData(e.target.files[0], function() {
                  alert(EXIF.getTag(this,"GPSLatitude"));
              });
          }

Please help me solve this issue as it is not building my ionic 2 application.

Angular Solutions


Solution 1 - Angular

You can cast it as a HTMLInputElement:

document.getElementById("customimage").onchange = function(e: Event) {
    let file = (<HTMLInputElement>e.target).files[0];
    // rest of your code...
}

Update:

You can also use this:

let file = (e.target as HTMLInputElement).files[0];

Solution 2 - Angular

The e.target property type depends on the element you are returning on getElementById(...). files is a property of input element: https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement

In this case, the TypeScript compiler doesn't know you are returning an input element and we dont have an Event class specific for this. So, you can create one like the following code:

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

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

Solution 3 - Angular

This is more lines, but I think it's the clearest.

    const onChange = (event: Event) => {
      const target= event.target as HTMLInputElement;
      const file: File = (target.files as FileList)[0];
      /** do something with the file **/
    };

2022 update: Some people have rightly pointed out that the two casts on the second line are unnecessary, this is totally correct and I've revised my answer.

	const onChange = (event: React.ChangeEvent) => {
		const target= event.target as HTMLInputElement;
		const file = target.files[0];
		/** do something with the file **/
	};

Solution 4 - Angular

const handleFileInput = (event: ChangeEvent) => {
        const target = event.target as HTMLInputElement;
        const file: File = (target.files as FileList)[0];
        /** do something with the file **/
    };

I would change Event to ChangeEvent, however the rest of Devin Clark's answer is great :)

Solution 5 - Angular

// use - ChangeEvent<HTMLInputElement>

document.getElementById("customimage").onchange= function(e?: ChangeEvent<HTMLInputElement>) {
            var files: any = e.target.files[0]; 
              EXIF.getData(e.target.files[0], function() {
                  alert(EXIF.getTag(this,"GPSLatitude"));
              });
          }

Solution 6 - Angular

const onChange => (event: Event): void {
	const input = event.target as HTMLInputElement;

	if (!input.files?.length) {
		return;
	}

	const file = input.files[0];
	console.log(file);
}

Solution 7 - Angular

I have found that:

<input type="file"  accept="image/*" 
(change)="upload($event)">

and

<ion-input type="file"  accept="image/*" 
(change)="upload($event)"><ion-input>  or (ionChange)

does not handle the event in the same way. Therefore event.target consists of different parameters.

I therefore did not use the ion-input tag, but the normal angular <input> tag with the (change)="upload($event)" trigger.

It worked for me on Ionic 4.

Solution 8 - Angular

Better avoid Type Casting whenever possible. Use e.currentTarget instead of e.target

Solution 9 - Angular

I just come to solved the same probleme, when I use :

e.target.files

It said that target doesnot have files property, so as you said in type script. You can also use :

e.target['files'][0]

It solved my probleme.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChirag ChaudhariView Question on Stackoverflow
Solution 1 - Angularsoroush gholamzadehView Answer on Stackoverflow
Solution 2 - AngularDiulleiView Answer on Stackoverflow
Solution 3 - AngularDevin ClarkView Answer on Stackoverflow
Solution 4 - AngulardarmouView Answer on Stackoverflow
Solution 5 - AngularskinnynpaleView Answer on Stackoverflow
Solution 6 - AngularnkitkuView Answer on Stackoverflow
Solution 7 - AngularTiaan OosthuizenView Answer on Stackoverflow
Solution 8 - AngulararudzevychView Answer on Stackoverflow
Solution 9 - AngularJohn DoeView Answer on Stackoverflow