1

I am developing a unity application that runs on a webpage, I want to upload a file to the server, with unity I can call any javascript function that is on the page.. I've hidden (with CSS) a form with a file input, my goal was to simulate a click on this input so that the open file dialog would open..

this is the form:

<form id="upload" method="post" action="upload.php" enctype="multipart/form-data">
    <input id="lol" type="file" name="upl" multiple/>
</form>

I tried doing this:

<script type="text/javascript">
    function testFunction(){
    //$("#lol").trigger('click');
    $("#lol").click();
    alert("...");
}
</script>

neither worked,

I can see the alert.. but no dialog is opened, shouldn't it work? I dont have much experience with JS or Jquery :(

thank you in advance.

EDIT: corrected the element I want (id=lol), it still does not work

DavidN
  • 111
  • 1
  • 9

1 Answers1

2

To trigger a click event on a file input without a direct user interaction is complicated because of security reasons. Actually it depends on your browser. Chrome for instance requires a visible input field. See this question for more information.

So unfortunately a general approach that works on every browser is not possible.

Community
  • 1
  • 1
  • 1
    Oh, I see this one is complicated. Actually It depends on your browser. Chrome for instance requires a visible input field. See [this question](http://stackoverflow.com/questions/210643/in-javascript-can-i-make-a-click-event-fire-programmatically-for-a-file-input) for more information. A general approach for every browser seems not possible because of security reasons :/ – Alexander Heimbuch Apr 12 '15 at 20:10
  • I couldn't find that question before making this one :( well, thank you for the answer zusatzstoff.. If you can, make it an answer so I can accept it, I can't accept the comment as the answer.. thank you for helping. – DavidN Apr 12 '15 at 20:19