I 'crafted' this piece of jQuery + html to accomplish the following:
There is a placeholder image that user can click, which causes a file selection dialog to open. Once a file is selected, the corresponding multipart form is uploaded to the server. I am trying to imitate AJAX behavior for this file upload, so I also use an invisible iframe to receive server response.
Let me present the code first, so it would be easier to explain the problem
jQuery("#myInput").change(function () { // Submit form upon file selection
// alert("myInput.val() = " + $('#myInput').val()); // alert 1
$('#form1').submit();
// alert("myInput.val() = " + $('#myInput').val()); // alert 2
});
<form id="form1" action="/do_upload.php" method="post" enctype="multipart/form-data" target="target_frame">
<input id="myInput" type="file" name="userfile" /><br>
</form>
<img src="/img/placeholder.png" onclick="$('#myInput').click();" >
<iframe id="target_frame" name="target_frame" src="#" style="width:0;height:0;border:0px solid #fff;"></iframe>
The code works perfectly on new Chrome/Firefox/Safari. (Interestingly it even works if I set visibility: hidden;
on myInput
. So apparently that's not much of a security concern). However both IE 9 and 10 show the same behavior: clicking the image brings up the dialog successfully, the file path is correctly set in "alert 1", but it is gone in "alert 2", and the form doesn't get submitted. On the other hand, clicking directly on the browse button of myInput
properly brings up the dialog and submits the form.
I am absolutely confused by how this behavior can even be possible! Any suggestions on how to fight off the annoying IE would be greatly appreciated :)