Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset selected file with input tag file type in Angular 2?

Tags:

angular

This is how my input tag looks like:

<input type="file" placeholder="File Name" name="filename" (change)="onChange($event)"> <button>Reset</button> 

I want to reset the selected file in Angular 2. Help would be greatly appreciated. Let me know if you need more details.

P.S.

I could get file details from $event parameters and save it in a typescript variable, but this variable is not bound to the input tag.

like image 300
Akash Avatar asked Oct 20 '16 22:10

Akash


People also ask

How to reset input type file field in Angular?

import { ViewChild } from '@angular/core'; ViewChild allows you to set a reference variable to your input, using that you can clear the value of input. After clearing the value of input using the reference variable, the selected file will be reset.

How do I reset the input on a file?

To reset a file input in React, set the input's value to null in your handleChange function, e.g. event. target. value = null . Setting the element's value property to null resets the file input.

How do you delete selected files on file upload control after a successful upload?

bind('focus', function() { // Clear any files currently selected in #upload-file $('#upload-file'). val(''); }) ; // Choose uploading new one - this works ok $('#upload-file'). bind('focus', function() { // Clear any files currently selected in #select-file $('#select-file').

How do I reset my ng model?

Using the ngModel If you are using [(ngModel)] directive to control your form input fields, then you can clear it by setting an empty string ( ' ' ) to the ngModel control property.


2 Answers

You can use ViewChild to access the input in your component. First, you need to add #someValue to your input so you can read it in the component:

<input #myInput type="file" placeholder="File Name" name="filename" (change)="onChange($event)"> 

Then in your component you need to import ViewChild from @angular/core:

import { ViewChild } from '@angular/core'; 

Then you use ViewChild to access the input from template:

@ViewChild('myInput') myInputVariable: ElementRef; 

Now you can use myInputVariable to reset the selected file because it's a reference to input with #myInput, for example create method reset() that will be called on click event of your button:

reset() {     console.log(this.myInputVariable.nativeElement.files);     this.myInputVariable.nativeElement.value = "";     console.log(this.myInputVariable.nativeElement.files); } 

First console.log will print the file you selected, second console.log will print an empty array because this.myInputVariable.nativeElement.value = ""; deletes selected file(s) from the input. We have to use this.myInputVariable.nativeElement.value = ""; to reset the value of the input because input's FileList attribute is readonly, so it is impossible to just remove item from array. Here's working Plunker.

like image 89
Stefan Svrkota Avatar answered Sep 21 '22 02:09

Stefan Svrkota


Angular 5

html

<input type="file" #inputFile>  <button (click)="reset()">Reset</button> 

template

@ViewChild('inputFile') myInputVariable: ElementRef;  reset() {     this.myInputVariable.nativeElement.value = ''; } 

Button is not required. You can reset it after change event, it is just for demonstration

like image 23
Admir Avatar answered Sep 17 '22 02:09

Admir