Load page and at the end click a button automatically

1

I would like to automatically click the fileToUpload button when loading the page, in the following code:

<body>

  <form name="form5" id="form1" enctype="multipart/form-data" method="post" action="Upload.aspx">

    <div>
      <input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();" accept="image/*" capture="camera" />
    </div>

    <div>
      <input type="button" onclick="uploadFile()" value="Upload" />
    </div>
  </form>
<!-- fiz esse código abaixo mas não funciona-->
<script type="text/javascript">
    document.form5.getElementById("fileToUpload").click();
</script>

</body>
    
asked by anonymous 23.11.2015 / 19:21

2 answers

1

This can not be crossbrowser for security reasons. Browsers are increasingly restricting access to files.

The only way to open this file selection box is following a user action. That is, if there is an event on the page it is possible to "transfer" it so that the window opens (example: link ) .

But it will not work if it is an event created without user action (example: link that does not work on Chrome 46 on mac) .

    
23.11.2015 / 19:54
0

Using the scripts below you can run the click event on a input of type file , but only on some browsers with old versions, as mentioned in this question of SOen. So it will not help you but stick to it if anyone needs it.

$(document).ready(function() {
    $('#fileToUpload').trigger('click');
});

document.addEventListener('DOMContentLoaded', function() {
   document.getElementById('fileToUpload').click();
});
    
23.11.2015 / 19:32