I have the following line in HTML:
<input type="file" #fileInput style="display: none" accept=".xml" (change)="OnFileSelected($event)"/>
Upon selecting a file, the OnFileSelected callback is invoked. But then, if I select the same file again, this callback is not called.
Can you please help?
onChange will not detect any change to input type file if the same file has been selected. There are 2 possible ways to make onChange working on same file selection which I would recommend
Either you need to add an event like onClick to clear the value so that the change event will work.
<input type="file" #fileInput style="display: none" accept=".xml" (change)="OnFileSelected($event)" (click)="this.value=null"/>
Add multiple attribute to the input element
<input type="file" #fileInput style="display: none" accept=".xml" (change)="OnFileSelected($event)" multiple/>
Hope this helps.
EDIT:
As suggested by others in comments, you can achieve it like below
<input type="file" #fileInput style="display: none" accept=".xml" (change)="OnFileSelected($event)" (click)="$event.target.value=null"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With