I have to open file upload dialog by clicking some other button i.e i am hiding file upload control(visibility:hidden) and on clicking of some other button i want to open that dialog. Below is the code which i am having:
<input type="file" style="visibility: hidden;" />
Below is the javascript:
$('#button').click(function() { $('input[type=file]').click(); });
It is working fine in Firefox 4 and IE8 but in chrome12 it is not working i.e the dialog is not being opened. Any idea why?
Use the type="file" Attribute in HTML and onchange Method in JavaScript to Open File Dialog. We will create a button element in the following instance, and an onclick attribute will follow this. This will trigger a function in the JavaScript file.
When the Button is clicked, it triggers the Click event of the Fileupload element which in turn opens the File Upload dialog to choose the File for uploading.
On click of for= attribute will automatically focus on "file input" and upload dialog box will open you need to add a little hack to achieve this. You can hide a file upload ( input type=file) behind your button. and onclick of your button you can trigger your file upload click.
Inside the window onload event handler, the HTML Image element has been assigned a Click event handler and the Fileupload element has been assigned Change event handler. When the Image is clicked, it triggers the Click event of the Fileupload element which in turn opens the File Upload dialog to choose the File for uploading.
Inside the jQuery document ready event handler, the HTML Button element has been assigned a Click event handler and the Fileupload element has been assigned Change event handler. When the Button is clicked, it triggers the Click event of the Fileupload element which in turn opens the File Upload dialog to choose the File for uploading.
Tested today the simple code given in the question and the situation has changed:
There is just one catch - on IE the .click() is a blocking operation while on other browsers it is not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With