Set value type = "file" via jquery

Note:

Below is an answer that reflects the status of outdated browsers in 2009. Now you can really set the value of the file input element dynamically / programmatically using JavaScript in 2017.

See the answer in this question for details, as well as a demo:
How to set file input value programmatically (for example: when dragging and dropping files)?

Without downloading (viewing) the file, I want to set the fileuploader value via the click button. Is this possible, or am I mistaken?
sample html:

 <input type="file" id="files" /> <input type="button" id="btnTest" value="Test"/> 

and jquery:

  $("#btnTest").click(function() { alert('test'); //$('#files').html('pp.img'); //$('#files').val('pp.img'); $('#files').text('pp.img') }); 

But this does not work for me.

+4
source share
3 answers

Well, I have to say that there is no cross-browser way to do this. Text that is not selected for the file is located in the part of the widget defined by the implementation and is completely browser dependent. and if necessary, you can simply use CSS to cover the text with something when the value attribute is empty.

+3
source

Is it possible?

Nope

This would be a serious security flaw if you could do this using JavaScript, since you could specify any file on the user's computer, so you are not allowed to set the file value of the input element.

+8
source

I do not think you are allowed to set the value at all. Because, if you were able, you could put C:\passwords.txt there and send the value automatically. Not very safe.

+3
source

All Articles