17

This is my html code to upload excel file

 <span class="control-fileupload" >
      <label for="file1" class="text-left">{{filePlaceHolder}}</label>
      <input type="file" id="file1" value="" (change)="openFile($event)" >
 </span>

But the problem is if i'm uploading same file twice the change function is not executing again because there is no change in input field.

Suppose i have uploaded abc.xls file once and there are some validation on this file and if i change the content of abc.xls and re upload it then change function is not re validating it again.

What changes i should make to work change function every time i upload a file whether file name is same or not.

I want to know how to write this click function in type script as i'm new to this.

user1881845
  • 371
  • 2
  • 7
  • 16
  • clear value when you handle the file selected – guramidev Oct 27 '17 at 08:45
  • How to achieve this please explain in detail – user1881845 Oct 27 '17 at 08:49
  • 1
    Possible duplicate of [HTML input file selection event not firing upon selecting the same file](https://stackoverflow.com/questions/12030686/html-input-file-selection-event-not-firing-upon-selecting-the-same-file) – CBroe Oct 27 '17 at 08:51
  • Or [How to detect input type=file “change” for the same file?](https://stackoverflow.com/questions/4109276/how-to-detect-input-type-file-change-for-the-same-file) Please do some basic research before asking! – CBroe Oct 27 '17 at 08:51
  • Problem is i need answer in angular 2 those solutions not working with angular 2 i already tried – user1881845 Oct 27 '17 at 08:53
  • @user1881845 added an answer – guramidev Oct 27 '17 at 09:12

1 Answers1

58

In angular 2 you can do it like this:

<span class="control-fileupload" >
      <label for="file1" class="text-left">{{filePlaceHolder}}</label>
      <input #fileInput type="file" id="file1" (click)="fileInput.value = null" value="" (change)="openFile($event)" >
 </span>

This way every time you click on file input it will clear it's value so even if you select the same file change will fire.

guramidev
  • 2,228
  • 1
  • 15
  • 19