26

Possible Duplicate:
Jquery trigger file input

I'm working on a form which allows users to upload images to a website. So far I have got a drag and drop solution working in Chrome and Safari. However I also need to support the action of users clicking a button and browsing for files in the traditional manner.

Similar to what this would do:

<input type="file" name="my_file">

However rather than having the clunky file description area and un-editable Browse button I would rather use something like this:

<input type="button" id="get_file">

My question therefore is how to I make this button open a file selection window and process the selection the same way that type="file" would work?

Cheers.


My Solution

HTML:

<input type="button" id="my-button" value="Select Files">
<input type="file" name="my_file" id="my-file">

CSS:

#my-file { visibility: hidden; }

jQuery:

$('#my-button').click(function(){
    $('#my-file').click();
});

Working in Chrome, Firefox, and IE7+ so far (haven't tried IE6).

Community
  • 1
  • 1
diggersworld
  • 12,770
  • 24
  • 84
  • 119
  • 2
    There are million others but don't have time to choose the best duplicate – Esailija May 29 '12 at 12:05
  • 1
    Check this http://stackoverflow.com/questions/7132553/jquery-trigger-how-can-i-trigger-the-browse-file-in-the-input-when-i-click-on-a. It might help you. – TRR May 29 '12 at 12:06
  • @Esailija where is he asking for jQuery? link but not dupe. – rlemon May 29 '12 at 12:07
  • 1
    Maybe you can hide the type="file" element and trigger it with jQuery? Just a thought... – bjornruysen May 29 '12 at 12:04
  • @rlemon tbh I only looked at first 4 google results, they were all for jquery so I gave up and just put something out there. The accepted answer for that duplicate doesn't use jQuery however – Esailija May 29 '12 at 12:08
  • http://jsfiddle.net/rlemon/CgbnC/ – rlemon May 29 '12 at 12:14
  • Cheers everyone. I will give these suggested links a go, I am using jQuery so those suggestions are fine too. – diggersworld May 29 '12 at 12:19

2 Answers2

35

You could use JavaScript and trigger the hidden file input when the button input has been clicked.

http://jsfiddle.net/gregorypratt/dhyzV/ - simple

http://jsfiddle.net/gregorypratt/dhyzV/1/ - fancier with a little JQuery

Or, you could style a div directly over the file input and set pointer-events in CSS to none to allow the click events to pass through to the file input that is "behind" the fancy div. This only works in certain browsers though; http://caniuse.com/pointer-events

Greg
  • 31,180
  • 18
  • 65
  • 85
  • hadn't heard of `pointer-events` before. Unfortunately not a great solution though as I want this to work in earlier versions of IE. The jsfiddle solution looks good though... I'll try it as well as the suggestions of those above. – diggersworld May 29 '12 at 12:57
  • Happy to help. You could have a separate bit of CSS for IE that hides the fancy div and shows the standard file input as a sort of fallback. – Greg May 29 '12 at 13:00
  • Careful with display:none on input file elements. https://tympanus.net/codrops/2015/09/15/styling-customizing-file-inputs-smart-way/ – lenord Feb 08 '18 at 20:21
  • why not just do the jQuery bit with vanilla javascript? why include an entirely additional library – oldboy Apr 06 '18 at 23:15
13

If you want to allow the user to browse for a file, you need to have an input type="file" The closest you could get to your requirement would be to place the input type="file" on the page and hide it. Then, trigger the click event of the input when the button is clicked:

#myFileInput {
    display:none;
}

<input type="file" id="myFileInput" />
<input type="button"
       onclick="document.getElementById('myFileInput').click()" 
       value="Select a File" />

Here's a working fiddle.

Note: I would not recommend this approach. The input type="file" is the mechanism that users are accustomed to using for uploading a file.

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • You can't fire click on input type file to open file dialog, maybe in firefox though – Esailija May 29 '12 at 12:13
  • Works for me in Chrome 19 / Linux – rlemon May 29 '12 at 12:15
  • Might be because the call stack starts with an user action like click though.. but still why does this work, it would be somewhat easy to fool someone into giving access to files – Esailija May 29 '12 at 12:16
  • @rlemon doesn't work in Safari 534.50. And didn't use to work in chrome either... chrome has failed me :( – Esailija May 29 '12 at 12:24