Using jQuery:
HTML Code:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('#uploadFile').click(function(e) {
$('#fileUploadField').click();
e.preventDefault();
});
});
</script>
<input type="file" name="something" style="display: none" id="fileUploadField" />
<a href="" id="uploadFile">Upload File</a>
This works for me in IE7, 8 and 9 (possible 6 too - didn't check).
Update
This doesn't work in the versions of Chrome/Safari for Windows/Opera that I'm using, perhaps due to security restrictions. I remember trying to do this in an earlier project and then it didn't work in ff (whatever version it was at the time). So basically this type of script may not work in all browsers, unless someone could come up with a cross browser trick...
2nd Update
Looked into this a bit more since I am in need of the same functionality, turns out browsers don't allow click events to be invoked on hidden elements. So instead of display: none on the input type file as in the above code use something like:
<input type="file" name="something" style="visibility:hidden; position:absolute" id="fileUploadField" />
Which seems to make it work in webkit browsers like chrome and safari. However it still doesn't work in Opera.
Last edited by Marik; 08-19-2011 at 11:01 PM..
|