views:

1626

answers:

5

I know that it's possible to replace the browse button, which is generated in html, when you use input tag with type="file".

I'm not sure what is the best way, so if someone has experience with this please contribute.

+11  A: 

The best way is to make the file input control almost invisible (by giving it a very low opacity - do not do "visibility: hidden" or "display: none") and absolutely position something under it - with a lower z-index.

This way, the actual control will not be visible, and whatever you put under it will show through. But since the control is positioned above that button, it will still capture the click events (this is why you want to use opacity, not visibility or display - browsers make the element unclickable if you use those to hide it).

This article goes in-depth on the technique.

levik
This seems so hacky, it's too bad there isn't a Better Way (tm).
TM
A: 

This isn't technically possible for security purposes, so the user cannot be misled.

However, there are a couple of workarounds - take a look at http://www.quirksmode.org/dom/inputfile.html for one example.

For the record, this question has already been asked here (where I gave the same answer).

Luke Bennett
+4  A: 

Browsers don't really like you to mess around with file inputs, but it's possible to do this. I've seen a couple of techniques, but the simplest is to absolutely position the file input over whatever you want to use as a button, and set its opacity to zero or near-zero. This means that when the user clicks on the image (or whatever you have under there) they're actually clicking on the invisible browse button.

For example:

<input type="file" id="fileInput">
<img src="...">

#fileInput{
    position: absolute;
    opacity: 0;
    -moz-opacity: 0;
    filter: alpha(opacity=0);
}
Dan
+1  A: 

If you don't mind using javascript you can set the opasity of the file-input to 0, place your styled control on top via z-index and send clickevents from your button to the file-input. See here for the technique: http://www.quirksmode.org/dom/inputfile.html

erlando
+2  A: 

You can use a Flash uploader like SWFupload to do this, as well.

ceejayoz