8

Note:

The answer(s) below reflect the state of legacy browsers in 2009. Now you can actually set the value of the file input element dynamically/programatically using JavaScript in 2017.

See the answer in this question for details as well as a demo:
How to set file input value programatically (i.e.: when drag-dropping files)?

Without upload(browse) a file I want to set a value of fileuploader through button click.Is it possible or I am in wrong way?
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 its not work for me .

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
4b0
  • 21,981
  • 30
  • 95
  • 142

3 Answers3

7

Is it possible?

nope

It would be a serious security flaw if you were able to do this via JavaScript, as you'd be able to specify any file on a user's computer, which is why you're not allowed to set the value of a file input element.

Community
  • 1
  • 1
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 2
    how about just inserting pure base64 image stream? i want to make a classic edit form and want to put my own default values to those image inputs, not users. – ulkas Jul 17 '14 at 08:42
5

well i must say that there is no cross-browser way to do this. The "no file selected" text is in the implementation-defined part of the widget, and it's totally browser specific. and if it is necessary You could simply use CSS to cover the text with something when the value attribute is empty.

rahul
  • 7,573
  • 7
  • 39
  • 53
3

I don't think you are allowed to set the value at all. Because if you were able to, you could put C:\passwords.txt in there and submit the value automatically. Not very secure.

Marc
  • 6,749
  • 9
  • 47
  • 78