Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open file upload dialog on click

Tags:

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?

like image 990
Niraj Choubey Avatar asked Jun 09 '11 12:06

Niraj Choubey


People also ask

How do I open a dialogue in HTML?

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.

How do I upload a file using the fileupload button?

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.

How to trigger a file upload when input type=file?

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.

How do I upload a file from an image in HTML?

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.

How to upload a file using jQuery document ready event handler?

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.


1 Answers

Tested today the simple code given in the question and the situation has changed:

  • IE9: works
  • Chrome23: works
  • Firefox15: works

There is just one catch - on IE the .click() is a blocking operation while on other browsers it is not.

like image 133
Knaģis Avatar answered Oct 11 '22 13:10

Knaģis