How do I customize the file upload,
<input type="file" />
To look the same in all browsers? I want to invoke file browser on clicking on my custom styled button.
How do I customize the file upload,
<input type="file" />
To look the same in all browsers? I want to invoke file browser on clicking on my custom styled button.
You can overload the style on a upload button yourself by setting the opacity of the file upload to 0 and putting a div on top with the style you want. e.g.
<input style="opacity:0; position: fixed;" onchange="openfile(event)" type="file">
<div class="icon">Open</div>
One option is to hide the real input and replace it with a button.
$('input[type=file]').each(function(index,input){
var button = $("<button class='file_btn'>Upload File</button>");
$(input).before(button);
$(input).css({position:'absolute',top:'-1000px'});
button.click(function(){
$(input).trigger('click');
});
});
A working fiddle: http://jsfiddle.net/KQhGR/1/
After thinking for half hour found a solution myself which just solves this problem.(Yes, I knew the answer when I posted this question, but wanted to see how others would solve this, and of course I want to share my solution with the rest)
Working jsfiddle example.
<input style="float:left; width:270px; height:25px;" id="path" name="path" disabled /> <!--disabled it, to avoid user from editing the file path-->
<input style="display:none;" type="file" id="photo-path" name="file" placeholder="" /> <!-- hide the default input type='file' coz its ugly :) -->
<button type="submit" onclick="document.getElementById('photo-path').click(); return false;">Browse</button> <!-- but call hidden input type='file' when user clicks on your custom browse button, -->
This is the JQuery code: (to copy file path to your custom field)
<script type='text/javascript'>
$('input[name="file"]').change(function(){
var fileName = $(this).val();
$('input[name="path"]').val(fileName);
});
</script>
Your best bet for something like this is to use a plugin like Uploadify to not only style the upload button consistently but add a progress bar adn multiple file uploads.
Unfortunately there isn't a reliable way (that I know of) to style file uploads consistently across all browsers. It's one of those controls front end developers don't like to talk about.