16

I know there are simmilar questions but none cover the method for Angular 5 or at least not in a way that I understand it.

For my image upload system I need to know if a picture is attached to the input tag and what name the file has. Here is my code:

HTML:

<input 
  type="file" 
  [(ngModel)]="currentInput"
  (change)="onFileSelected($event)"
>

Angular:

export class ImageUpload {
    currentInput;

    onFileSelected(event) {
        console.log(this.currentInput);
    }
}

No matter if there is a file attached or not the value in "currentInput" is always undefined. How does this work with input when the type is equal to "file"?

Thanks!

2
  • Tried your code, works fine for me, Check this out => stackblitz.com/edit/angular-kl2y1z Commented Jul 26, 2018 at 10:42
  • well in my example I get undefined.. Commented Jul 26, 2018 at 10:55

5 Answers 5

31

Try this below way

onFileSelected(event) {
 if(event.target.files.length > 0) 
  {
    console.log(event.target.files[0].name);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

5

@ramesh-rajendran's answer is good. If you want the TypeScript solution:

onFileSelected(event: Event) {
    const target = event.target as HTMLInputElement;
    if (target?.files?.length) {
        console.log(target.files[0].name);
    }
}

1 Comment

FYI: Just a minor change, the if condition can be (target?.files?.length) which will work same as (target.files && target.files.length > 0). Thanks
0

Give a name to input field, it is a requirement: https://angular.io/guide/forms#!#ngModel. Also, you have defined the function outside of class. function and property both need to be inside a class.

Update: Data binding not supported with file input types. It need to be done using pure javascript.

<input 
  type="file" 
  name = "currentInput"
  [(ngModel)]="currentInput"
  (change)="onFileSelected($event)"
>

export class ImageUpload {
  currentInput:any;
  onFileSelected(event) {
    console.log(event.target.files);
    this.currentInput = event.target.files; 
  }
}

3 Comments

doesn't change anything
Thanks for the update but that was just a typo on my behalf, your solution still doesn't do anything
Yes data binding not supported with file input types. It need to be done using pure javascript.
0

//In your component, create a function that emits an event on file selected
import {Component, OnInit, EventEmitter} from '@angular/core';

public onFileSelected(event: EventEmitter<File[]>) {
    const file: File = event[0];
    console.log(file);
}
// In your html, attach the function to the input tag like so
<input type="file" id="file" (change)="onFileSelected($event)">

2 Comments

You should add explanation.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

Try the code below. It uses event emitter which listens to input changes and returns the file object, along with its metadata. Give it a try. What I like is that you don't need an external library.

//In your component, create a function that emits an event on file selected
import {Component, OnInit, EventEmitter} from '@angular/core';

public onFileSelected(event: EventEmitter<File[]>) {
    const file: File = event[0];
    console.log(file);
}
// In your html, attach the function to the input tag like so
<input type="file" id="file" (change)="onFileSelected($event)">

1 Comment

The event emitted from (change) handled is NOT of type [File[]] - above code doesn't output correct file in the log

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.