30

Possible Duplicate:
In JavaScript can I make a “click” event fire programmatically for a file input element?

I've got a web page that looks like this

<html>
    <head>
        <title>File Upload Click Test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    </head>
    <body>
        <div onclick="$('input[type=file]').click()" >CLICK SIMULATOR</div>
        <input type="file"></input>
    </body>
</html>

My goal is to have the div raise a click event on the file input, and this seems to work exactly as I'd expect in IE and Chrome, but doesn't work in Firefox (no file browser is opened when you click on the div).

Is there a way to get this working in FF?

4

2 Answers 2

34

Is there a way to get this working in FF?

No, and it doesn't work in most common versions of IE, either. IE will open the dialog, but once you've selected a file with it the form won't actually submit.

Abandon hope. The only way to fake a file upload box is using the transparency technique, and that's really not recommended at all as browsers may lay out file upload boxes differently internally (or even provide a file upload control that doesn't include a Browse dialogue), making it highly likely you'll end up with an inoperable form.

Learn to love the grey file upload field, or use progressive enhancement to replace it with Flash where available.

Sign up to request clarification or add additional context in comments.

4 Comments

Abandon hope. Check.
It's working now in Firefox 4.
Well thanks for not allowing me to waste any more time.. already spent a couple days on this with hope in hand.
Thanks for this, I was stuck for awhile as I was implementing file uploading with ajax using an iframe method to support older IE versions. After lots of hitting my head I stripped my form down to find the ajax method with the iframe was working correctly. I found it was actually this issue, as I had a click event firing for a paper clip image. Solution was to just adjust the opacity of the input (instead of it being hidden) as seen: stackoverflow.com/questions/210643/… :)
22

Here's the workaround (FF). The HTML bit:

<label>Upload Business Logo</label>
<input type="file" name="logo" id="logo" class="file-upload" />
<input type="text" id="text-logo" class="text-upload" readonly/>
<input type="button" id="btn-logo" class="btn-upload" alt="" />

CSS for the input file type:

.file-upload { display:none; }

The jQuery bit:

//bind click
$('#btn-logo').click(function(event) {
  $('#logo').click();
});

//capture selected filename
$('#logo').change(function(click) {
  $('#text-logo').val(this.value);
});

Upon form submit, the hidden input file will upload the file. Hope this helps :)

6 Comments

Did you try the code? If it were that easy, why would the asker be here? Why would I be here? Last I checked, you can't set the value of a file input. And apparently you can't trigger events on them either. Probably security reasons.
I take it back, looks like it is that simple. But it doesn't work when trying it in the console. When I place the code in my document, it seems to work just fine. Derp.
+1 this works perfectly, just not in the console! Weird, I guess it's for security purposes.
This works so easily! I think the confusion is in console that doesn't reflect same. By the way! a +1 for you :)
Here's a jsfiddle of this code
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.