<form action="uploadScript.php" method="post" enctype="multipart/form-data"><div><!-- filename to display to the user --><p id="file-name" class="margin-10 bold-10"></p>
<!-- Hide this from the users view with css display:none; --><input class="display-none" id="file-type" type="file" size="4" name="file"/>
<!-- Style this button with type image or css whatever you wish --><input id="browse-click" type="button" class="button" value="Browse for files"/>
<!-- submit button --><input type="submit" class="button" value="Change"/></div>
$(window).load(function () {var intervalFunc = function () {$('#file-name').html($('#file-type').val());};$('#browse-click').on('click', function () { // use .live() for older versions of jQuery$('#file-type').click();setInterval(intervalFunc, 1);return false;});});
<form action="#type your action here" method="POST" enctype="multipart/form-data"><div id="yourBtn" style="height: 50px; width: 100px;border: 1px dashed #BBB; cursor:pointer;" onclick="getFile()">Click to upload!</div><!-- this is your file input tag, so i hide it!--><div style='height: 0px;width:0px; overflow:hidden;'><input id="upfile" type="file" value="upload"/></div><!-- here you can have file submit button or you can write a simple script to upload the file automatically--><input type="submit" value='submit' ></form>
then use this simple script to pass the click event to file input tag.
function getFile(){document.getElementById("upfile").click();}
<form action="#type your action here" method="POST" enctype="multipart/form-data" name="myForm"><div id="yourBtn" onclick="getFile()">click to upload a file</div><!-- this is your file input tag, so i hide it!--><!-- i used the onchange event to fire the form submission--><div style='height: 0px;width: 0px; overflow:hidden;'><input id="upfile" type="file" value="upload" onchange="sub(this)" /></div><!-- here you can have file submit button or you can write a simple script to upload the file automatically--><!-- <input type="submit" value='submit' > --></form>
<div id='wrapper'><input type='file' id='browse'></div><style>#wrapper {width: 93px; /*play with this value */height: 28px; /*play with this value */background: url('browseBtn.png') 0 0 no-repeat;border:none;overflow:hidden;}
#browse{margin-left:-145px; /*play with this value */opacity:0; /* set to .5 or something so you can better position it as an overlay then back to zero again after you're done */-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);}</style>
WebKit provides a hook for its file input button with the ::-webkit-file-upload-button pseudo-element. Again, pretty much any CSS rule can be applied, therefore the Trident example will work here as well:
.filename element will be used to display the selected file
below is the commented JS code needed (using jQuery) to make it work:
var $attach = $('#attach-project-file'),$remove = $('#remove-project-file'),$name = $('#attached-project-file');
// initially hide the remove button$remove.hide();
// do this when file input has changed// i.e.: a file has been selected$attach.on('change', function() {var val = $(this).val();if (val !== '') {// if value different than empty
// show the file name as text// hide/text/fadeIn creates a nice effect when changing the text$name.hide().text(val).fadeIn();
// show the remove button$remove.fadeIn();} else {// if value empty, means the file has been removed
// show the default text$name.hide().text('Click to select a file').fadeIn();
// hide remove button$remove.fadeOut();}});
// remove selected file when clicking the remove button// prevent click bubbling to the parent label and triggering file selection$remove.on('click', function(e) {e.preventDefault();e.stopPropagation();
$attach.val('').change(); // trigger change event});
// div styled as my load file button<div id="simClick">Load from backup</div>
<input type="file" id="readFile" />
// Click function for input$("#readFile").click(function() {readFile();});
// Simulate click on the input when clicking div$("#simClick").click(function() {$("#readFile").trigger("click");});