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)?

I need to fill an HTML form with JavaScript to set the path of a file for upload it. The web that this form belongs to, already has a visual upload implemented, but

<form id="mobile_fileform">
    <input type="file" style="height: 20px; width: 0px; opacity: 0; " id="mobile_fileselect1" multiple="" size="-17">
    <input type="file" style="height: 20px; width: 0px; opacity: 0; " id="mobile_fileselect2" multiple="" size="-17">
    <input type="file" style="height: 20px; width: 0px; opacity: 0; " id="mobile_fileselect3" multiple="" size="-17">
</form>

How can I set the path of the file in this form and how can I upload it using JavaScript?

Thanks for your help

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
isiaatz
  • 1,125
  • 4
  • 15
  • 22
  • Possible duplicate [stackoverflow.com/questions/4286782/...](http://stackoverflow.com/questions/4286782/how-to-make-input-type-file-editable)... – War10ck Apr 02 '13 at 12:22
  • For security reasons, it is not possible to manually add and upload a file in a file input control with JavaScript. – War10ck Apr 02 '13 at 12:25

1 Answers1

11

The value property of input:file is read-only for security reasons. If you want to set it, you'll need to do it server-side.

As a result, what you're asking to do is not possible. Consider the implications: any webpage would be able to upload any file from someone's computer, so long as they knew the path.

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • This answer is obsolete. Read this answer instead: https://stackoverflow.com/questions/47119426/how-to-set-file-objects-and-length-property-at-filelist-object-where-the-files-a#answer-47172409 – derekbaker783 May 26 '20 at 22:24
  • – Mahmoud Oct 16 '22 at 12:04