13

I'm trying to make a binding to an input field type file through ngModel on the typical Angular way like this:

<input type="file" id="fileUpload" [(ngModel)]="file">

and

files:any

My problem is that after I have chosen a file, the value of my variable files is still undefined Here an example with stackblitz: https://stackblitz.com/edit/angular-6mbdww

Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
Leonzen
  • 1,195
  • 5
  • 13
  • 30
  • 2
    Possible duplicate of [Angular 2 File upload from input type=file](https://stackoverflow.com/questions/35399617/angular-2-file-upload-from-input-type-file) – Akash Agrawal Jan 18 '18 at 12:03

1 Answers1

19

You have to do it externally through (change) event

<input (change)="onFileChange($event)" type="file" id="fileUpload">

And access in the ts file like the below code

  files: any[];

  onFileChange(event){
    this.files = event.target.files;
    console.log(event);
  }
Ashraful Islam
  • 1,228
  • 10
  • 13