I want to achieve gmail like upload functionality which is upload on click/event, which works well on all browsers. For IE8 and below gmail is using Flash to upload file, but for IE9 + they are using single click upload without Flash. On click of div gmail is calling browse popup and on select of image or file in browse popup, submit gets triggered programmatically.
I have working sample of my code, which works well in Chrome, Firefox and IE 10 + browsers, but it does not work in IE9. I am not looking for IE8 solution as that can be managed with Flash.
I am aware that because of security reasons IE restricts programmatic form submit on File input. I have seen many solutions for upload at single click in which browse button is hidden and overlapped on another anchor and on click of that anchor, browse gets open and upload is done but that is not what I am looking for. I want to do it programmatically.
My Question is how gmail achieve this in IE9 which works, but my code does not work. Can anybody help me in achieving same functionality which works well in IE9.
Here is jsFiddle output:
http://jsfiddle.net/vikramkute/nnJ8q/show/
For FireFox, Chrome and IE10+
- Step1: Select a file from drop down browse popup gets open.
- Step2: Select Image or any file.
- Output: You will get redirected to "http://jsfiddle.net/vikramkute/nnJ8q/show/upload_file.php" (That means server side script gets called and file uploaded)
For IE9
- Step1: Select a file from drop down browse popup gets open.
- Step2: Select Image or any file.
- Output: You will not get redirected (That means form submit dose not get called.)
Here is jsFiddle code: http://jsfiddle.net/vikramkute/nnJ8q
Try gmail upload in IE9. It works.
Here is my code:
$(function() {
$("#files").on("change", function(){
var selected = $(this).val();
$('input[type=file]').trigger('click');
})
$('input[type=file]').on("change", function(){
$("#uploadForm").trigger('submit');
})
});
HTML:
<select id="files">
<option value="blank">(blank)</option>
<option value="file1">File1</option>
<option value="file2">File2</option>
<option value="file3">File3</option>
<option value="file4">File4</option>
</select>
<form id="uploadForm" action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" style="visibility:hidden;" >
<input type="submit" value="Submit" id="uploadSubmit" style="visibility:hidden;"/>
</form>