angular icon indicating copy to clipboard operation
angular copied to clipboard

Get files property of <input type="file"> Element

Open JanHuege opened this issue 10 years ago • 13 comments

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...

JanHuege avatar Dec 22 '15 15:12 JanHuege

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.

wesleycho avatar Dec 24 '15 05:12 wesleycho

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

robwormald avatar Dec 24 '15 05:12 robwormald

@wesleycho No, atm I am waiting for a change on and then I call the codesnippet from above @robwormald okay thanks

JanHuege avatar Dec 24 '15 12:12 JanHuege

@robwormald I can't believe how long I have been searching for that simple solution, thanks very much.

fToledo29 avatar May 20 '17 19:05 fToledo29

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

rozester avatar Nov 27 '17 13:11 rozester

well said (correct)

sanjayror avatar Dec 20 '17 11:12 sanjayror

Thank very much

leparfait avatar Mar 12 '18 13:03 leparfait

I cannot use the above-mentioned link to plnkr. Can someone please provide the solution.

Surbhiairan avatar Mar 22 '18 11:03 Surbhiairan

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

and then update your model inside upload2() function ==> can u please elaborate this?

ghost avatar Sep 14 '18 11:09 ghost

but i didi not use[{ ngModel)] <input type="file" (change)="uploadMedia($event)" class="hide_button" #fileInputC>

aparnatech avatar Apr 03 '19 08:04 aparnatech

but i didi not use[{ ngModel)] <input type="file" (change)="uploadMedia($event)" class="hide_button" #fileInputC>

but it is not working

aparnatech avatar Apr 03 '19 08:04 aparnatech

<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.

abdurrahmanyildirim avatar Aug 23 '19 10:08 abdurrahmanyildirim

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.