7

The problem is that I have a file input field that takes only one file at a time, I need it to be like this.

If I try to upload one file, everything is fine. But if I need to upload more files it seems like the "change" handler method is not being called unless I reload the page, which is not what anyone would want.

The HTML looks like this:

<div class="col-xs-7"> <button class="btn btn-primary" [disabled]="isLoadingModal" (click)="activityFileInput.click()"> archivo</button> {{ newActivity.fileName }} <input type="file" id="activity-file-input" [disabled]="isLoadingModal" (change)="selectFile(activityFileInput.files[0])" #activityFileInput hidden> </div> 

The function in the component is:

selectFile(file: File): void { console.log('Hello'); if(file == undefined) return; this.newActivity.fileName = file.name; this.newActivity.file = file; } 

On the first file upload the "Hello" shows, no problem. But the method doesn't seem to be called more than once. How can this be solved?

2
  • How do you manage isLoadingModal? Is it always false? Commented Mar 20, 2018 at 0:24
  • Right before calling the server I set to true, and when I get a response i set it back to false. Just UI stuff right there. Commented Mar 20, 2018 at 0:25

2 Answers 2

17

Set the value of the input to null in the onclick event...

<input type="file" id="activity-file-input" onclick="this.value = null" [disabled]="isLoadingModal" (change)="selectFile(activityFileInput.files[0])" #activityFileInput hidden> 
Sign up to request clarification or add additional context in comments.

1 Comment

setting the value to null worked fine for me. Thank you :)
-1

it happens only when you try to upload the same file/image. When (change) sees the same event, it doesn't trigger the given method. To fix it add (click)="$event.taget.value=null" in your input tag

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.