3

Note:

The answers & comments 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'm currently working on programmatically filling an HTML/CSS/jquery form on another website using JavaScript. I'm able to fill all textboxs and click necessary buttons easily,but I'm stuck trying to set the value of the file chooser. I'm aware JavaScript doesn't have permissions to set the value of a file chooser, and it appears I can't control the dialog that appears when the button is pressed.

Is there some other way I can automate this? Or is there a solution in JavaScript?

Thanks!

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Nat
  • 890
  • 3
  • 11
  • 23
  • 3
    So basically you want to program your browser to fill the form and send it ? Maybe you should take a look at [CasperJS](http://casperjs.org/) which is a web browser that you can script in JS. – tforgione Oct 28 '15 at 14:48
  • You want to specify for the user which file to upload (security risk)? @DragonRock Can Casper upload files? – Ruan Mendes Oct 28 '15 at 14:49
  • @JuanMendes I think so, apparently [this guy](http://stackoverflow.com/questions/20941888/issues-getting-casperjs-to-upload-image-to-file-field-tried-casperjs-fill-an) managed to do it – tforgione Oct 28 '15 at 14:51
  • I should mention the file is constant, I can provide the file path as a string. I'll check out casperJS, but it sounds like it.might suffer the same permissions issues! – Nat Oct 28 '15 at 14:52
  • @Nathan you're sure ? CasperJS allows you to describe what you want your browser to do, and then, he does it. CasperJS scripts won't be sent to anyone, you just run them from your terminal, so I'm not sure that there will be security concerns. – tforgione Oct 28 '15 at 14:54
  • I'll be sure to try it after this class. You should submit an answer :) – Nat Oct 28 '15 at 14:56
  • you can fill this tag just in server side : http://stackoverflow.com/questions/15763941/fill-input-file-form-with-javascript – Tahir Bencheikh Oct 28 '15 at 15:53
  • duplicate?: [how-to-set-a-value-to-a-file-input-in-html](http://stackoverflow.com/questions/1696877/how-to-set-a-value-to-a-file-input-in-html) – Paul Bönisch Oct 28 '15 at 16:24

0 Answers0