Get files property of <input type="file"> Element
I am running into this:
EXCEPTION: InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.
Using this to get the file from the Element:
var inputImage: any = document.getElementById("upload");
this.wine.imageUrl = inputImage.files[0];
Typescript tells me there is no files property on HTMLElement but there is...
This needs a reproduction - if I had to guess what is happening, you are likely doing <input type="file" [value]="foo">, which is invalid for a file input since the browser only allows you to read, not write to the input.
also, this is probably the wrong way to go about this in angular2, unless you have some crazy-specific use case. Better to use something like this http://plnkr.co/edit/fd6QWBxHLlQRlY7vv3Fl?p=preview
@wesleycho No, atm I am waiting for a change on and then I call the codesnippet from above @robwormald okay thanks
@robwormald I can't believe how long I have been searching for that simple solution, thanks very much.
I had this issue with angular 4, and the solution was very easy
if you use this code:-
<input type="file" [(ngModel)]="Attachment" (change)="upload2()" placeholder="Upload file..." />
it causes a problem because of two way binding in this [(ngModel)]="Attachment", it is a security issue when you change the value of input file without using the browser or using code behind
so you should use this istead
<input type="file" (change)="upload2()" placeholder="Upload file..." />
and then update your model inside upload2() function
well said (correct)
Thank very much
I cannot use the above-mentioned link to plnkr. Can someone please provide the solution.
I had this issue with angular 4, and the solution was very easy if you use this code:-
<input type="file" [(ngModel)]="Attachment" (change)="upload2()" placeholder="Upload file..." />it causes a problem because of two way binding in this [(ngModel)]="Attachment", it is a security issue when you change the value of input file without using the browser or using code behindso you should use this istead
<input type="file" (change)="upload2()" placeholder="Upload file..." />and then update your model inside upload2() function
and then update your model inside upload2() function ==> can u please elaborate this?
but i didi not use[{ ngModel)] <input type="file" (change)="uploadMedia($event)" class="hide_button" #fileInputC>
but i didi not use[{ ngModel)] <input type="file" (change)="uploadMedia($event)" class="hide_button" #fileInputC>
but it is not working
<input #fileInput style="display: none" type="file" (change)="onFileSelected($event)"> <button class="btn btn-primary form-control" (click)="fileInput.click()"> I did like this, it is worked.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.