This is how my input tag looks like:
One way to achieve it is to wrap your input in <form>
tag and reset it.
I'm not considering attaching thr form to NgForm
or FormControl
either.
@Component({
selector: 'form-component',
template: `
<form #form>
<input type="file" placeholder="File Name" name="filename">
</form>
<button (click)="reset()">Reset</button>
`
})
class FormComponent {
@ViewChild('form') form;
reset() {
this.form.nativeElement.reset()
}
}
https://plnkr.co/edit/Ulqh2l093LV6GlQWKkUA?p=preview
I am using a a very simple aproach. After a file has been uploaded, i shortly remove the input control, using *ngIf. That will cause the input field being removed from the dom and re-added, consequencely it is a new control, and therefore it is emply:
showUploader: boolean = true;
async upload($event) {
await dosomethingwiththefile();
this.showUploader = false;
setTimeout(() =>{ this.showUploader = true }, 100);
}
<input type="file" (change)="upload($event)" *ngIf="showUploader">
If you are facing issue with ng2-file-upload,
HTML:
<input type="file" name="myfile" ` **#activeFrameinputFile** `ng2FileSelect [uploader]="frameUploader" (change)="frameUploader.uploadAll()" />
component
import { Component, OnInit, ElementRef, ViewChild } from '@angular/core';
@ViewChild('`**activeFrameinputFile**`') `**InputFrameVariable**`: ElementRef;
this.frameUploader.onSuccessItem = (item, response, status, headers) => {
this.`**InputFrameVariable**`.nativeElement.value = '';
};
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.
I think its simple, use #variable
<input #variable type="file" placeholder="File Name" name="filename" (change)="onChange($event); variable.value='' ">
<button>Reset</button>
"variable.value = null" option also available
template:
<input [(ngModel)]="componentField" type="file" (change)="fileChange($event)" placeholder="Upload file">
component:
fileChange(event) {
alert(this.torrentFileValue);
this.torrentFileValue = '';
}
}