views:

476

answers:

3

Hi,
Like a lot of people, I'd like to customize the ugly input type=file, and I know that it can't be done without some hacks and/or javascript. But, the thing is that in my case the upload file buttons are just for uploading images (jpeg|jpg|png|gif), so I was wondering if I could use a "clickable" image which would act exactly as an input type file (show the dialog box, and same $_FILE on submitted page).
I found some workaround here, and this interesting one too (but does not work on Chrome =/).

What do you guys do when you want to add some style to your file buttons? If you have any point of view about it, just hit the answer button ;)

Cheers,
Nicolas

+1  A: 

I would use SWFUpload or Uploadify. They need Flash but do everything you want without troubles.

Any <input type="file"> based workaround that tries to trigger the "open file" dialog by means other than clicking on the actual control could be removed from browsers for security reasons at any time. (I think in the current versions of FF and IE, it is not possible any more to trigger that event programmatically.)

Pekka
Thanks, I've seen them around too, but I think that both of them need a flash player to work, and I do not want to add another mandatory layer to users. Plus are you sure that they will send the exact same type of data as the input file ones do? I do not want to retest all my PHP done in the submit page. Cheers.
Nicolas
@niko no, they work differently, they are not form elements but send the file to a receiving script on their own, so you would probably have to change your script's workflow. If that's not an option, I think you can't do better than Shaun Inman did in the post you link to...
Pekka
I'm afraid so then, that's really a shame. Isn't there anything about it in future CSS implementation? Thanks for the replies anyway :)
Nicolas
@niko you're welcome. Good question about the CSS - I don't know actually, but HTML 5 may well bring new things here. Check out http://www.appelsiini.net/2009/10/html5-drag-and-drop-multiple-file-upload for example.
Pekka
Yeah it seems promising even if the demo http://www.appelsiini.net/demo/html5_upload/demo.html does not display with my FF 3.6.3 whereas it says I do need FF 3.6.x Anyway, to go back to my issue, currently HTML/CSS/Javscript (but no flash) only allow me to do what? Some fonts and "color" customization? Am I right then? Cheers.
Nicolas
@niko here's a good overview on workarounds: http://www.quirksmode.org/dom/inputfile.html with the limitation that they don't work on Chrome - I don't know what the state of things is there.
Pekka
Yeah I forgot to mention that page, which could be the better solution in my case, but the issue with erasing the image chosen plus some limitations on old browsers make me a little reluctant. Btw I don't see any trouble on Chrome 4.1 :)
Nicolas
A: 

technically, you can upload files only using a file input field (unless you use flash, java etc)

And you can't trigger it's click via script, either.

so you will have to actually display the element to the user and your only hope is to hack the CSS to make the field appear as a button, but I don't think it's possible to hide the user input field without deactivating the control

seanizer
Yeah it seems so. I kinda came to the same conclusion unfortunately. Do you know any CSS hacks to make it at least look "better"? Cheers. (ps: I don't have enough reputation to vote for your reply, but it is usefull)
Nicolas
I don't think such hacks are even possible. You might be able to position another layer above just the text field part, but success is not guaranteed as I am pretty sure there is no defined layout for a file input field
seanizer
I agree there's not defined layout for a file input field (and its button). For example check a page in Chrome and in FF... that really is amazing how it looks different.
Nicolas
+1  A: 

Actually it can be done in pure css and it's pretty easy...

<label class="filebutton">
Browse For File!
<span><input type="file" id="myfile" name="myfile"></span>
</label>

label.filebutton {
    width:120px;
    height:40px;
    overflow:hidden;
    position:relative;
    background-color:#ccc;
}

label span input {
    z-index: 999;
    line-height: 0;
    font-size: 50px;
    position: absolute;
    top: -2px;
    left: -700px;
    opacity: 0;
    filter: alpha(opacity = 0);
    -ms-filter: "alpha(opacity=0)";
    cursor: pointer;
    _cursor: hand;
    margin: 0;
    padding:0;
}

The idea is to position the input absolutely inside your label. set the font size of the input to something large, which will increase the size of the "browse" button. It then takes some trial and error using the negative left / top properties to position the input browse button behind your label.

When positioning the button, set the alpha to 1. When you've finished set it back to 0 (so you can see what you're doing!)

Make sure you test across browsers because they'll all render the input button a slightly different size.

You can see an example here (Add Track button): http://rakmastering.com/upload/

Toby