Is it possible to change the appearance of <input type="file">
?
This question is related to
html
css
file-upload
input
Something like that maybe?
<form>
<input id="fileinput" type="file" style="display:none;"/>
</form>
<button id="falseinput">El Cucaratcha, for example</button>
<span id="selected_filename">No file selected</span>
<script>
$(document).ready( function() {
$('#falseinput').click(function(){
$("#fileinput").click();
});
});
$('#fileinput').change(function() {
$('#selected_filename').text($('#fileinput')[0].files[0].name);
});
</script>
<label for="fusk">dsfdsfsd</label>
<input id="fusk" type="file" name="photo" style="display: none;">
why not? ^_^
See the example here
In webkit you can try this out...
input[type="file"]::-webkit-file-upload-button{
/* style goes here */
}
The trick is hide the input and customize the label.
HTML:
<div class="inputfile-box">
<input type="file" id="file" class="inputfile" onchange='uploadFile(this)'>
<label for="file">
<span id="file-name" class="file-box"></span>
<span class="file-button">
<i class="fa fa-upload" aria-hidden="true"></i>
Select File
</span>
</label>
</div>
CSS:
.inputfile-box {
position: relative;
}
.inputfile {
display: none;
}
.container {
display: inline-block;
width: 100%;
}
.file-box {
display: inline-block;
width: 100%;
border: 1px solid;
padding: 5px 0px 5px 5px;
box-sizing: border-box;
height: calc(2rem - 2px);
}
.file-button {
background: red;
padding: 5px;
position: absolute;
border: 1px solid;
top: 0px;
right: 0px;
}
JS:
function uploadFile(target) {
document.getElementById("file-name").innerHTML = target.files[0].name;
}
You can check this example: https://jsfiddle.net/rjurado/hnf0zhy1/4/
Here is a quick pure CSS workaround (works on chrome and has a FireFox fallback included), including the file name,a label and an custom upload button, does what it should - no need of JavaScript at all!
Note: ? This works on Chrome and has a FireFox fallback - anyways, I would not use it on a real world website - if browser compatibility is a thing to you (what it should be). So it's more kind of experimental.
.fileUploadInput {_x000D_
display: grid;_x000D_
grid-gap: 10px;_x000D_
position: relative;_x000D_
z-index: 1; }_x000D_
_x000D_
.fileUploadInput label {_x000D_
display: flex;_x000D_
align-items: center;_x000D_
color: setColor(primary, 0.5);_x000D_
background: setColor(white);_x000D_
transition: .4s ease;_x000D_
font-family: arial, sans-serif;_x000D_
font-size: .75em;_x000D_
font-weight: regular; }_x000D_
_x000D_
.fileUploadInput input {_x000D_
position: relative;_x000D_
z-index: 1;_x000D_
padding: 0 gap(m);_x000D_
width: 100%;_x000D_
height: 50px;_x000D_
border: 1px solid #323262;_x000D_
border-radius: 3px;_x000D_
font-family: arial, sans-serif;_x000D_
font-size: 1rem;_x000D_
font-weight: regular; }_x000D_
.fileUploadInput input[type="file"] {_x000D_
padding: 0 gap(m); }_x000D_
.fileUploadInput input[type="file"]::-webkit-file-upload-button {_x000D_
visibility: hidden;_x000D_
margin-left: 10px;_x000D_
padding: 0;_x000D_
height: 50px;_x000D_
width: 0; }_x000D_
_x000D_
.fileUploadInput button {_x000D_
position: absolute;_x000D_
right: 0;_x000D_
bottom: 0;_x000D_
width: 50px;_x000D_
height: 50px;_x000D_
line-height: 0;_x000D_
user-select: none;_x000D_
color: white;_x000D_
background-color: #323262;_x000D_
border-radius: 0 3px 3px 0;_x000D_
font-family: arial, sans-serif;_x000D_
font-size: 1rem;_x000D_
font-weight: 800; }_x000D_
.fileUploadInput button svg {_x000D_
width: auto;_x000D_
height: 50%; }_x000D_
_x000D_
* {_x000D_
margin: 0px;_x000D_
padding: 0px;_x000D_
box-sizing: border-box;_x000D_
border: 0px;_x000D_
outline: 0;_x000D_
background-repeat: no-repeat;_x000D_
appearance: none;_x000D_
border-radius: 0;_x000D_
vertical-align: middle;_x000D_
font-weight: inherit;_x000D_
font-style: inherit;_x000D_
font-family: inherit;_x000D_
text-decoration: none;_x000D_
list-style: none;_x000D_
user-select: text;_x000D_
line-height: 1.333em; }_x000D_
_x000D_
body {_x000D_
display: flex;_x000D_
align-items: center;_x000D_
justify-content: center;_x000D_
width: 100%;_x000D_
height: 100vh;_x000D_
background: rgba(66, 50, 98, 0.05); }_x000D_
_x000D_
.container {_x000D_
padding: 25px;_x000D_
box-shadow: 0 0 20px rgba(66, 50, 98, 0.35);_x000D_
border: 1px solid #eaeaea;_x000D_
border-radius: 3px;_x000D_
background: white; }_x000D_
_x000D_
@-moz-document url-prefix() {_x000D_
.fileUploadInput button{_x000D_
display: none_x000D_
}_x000D_
}
_x000D_
<!-- Author: Ali Soueidan-->_x000D_
<!-- Author URI: https//: www.alisoueidan.com-->_x000D_
_x000D_
<div class="container">_x000D_
<div class="fileUploadInput">_x000D_
<label>? Upload File</label>_x000D_
<input type="file" />_x000D_
<button>+</button></div>_x000D_
</div>
_x000D_
Here is one way which I like because it makes the input fill out the whole container. The trick is the "font-size: 100px", and it need to go with the "overflow: hidden" and the relative position.
<div id="upload-file-container" >
<input type="file" />
</div>
#upload-file-container {
width: 200px;
height: 50px;
position: relative;
border: dashed 1px black;
overflow: hidden;
}
#upload-file-container input[type="file"]
{
margin: 0;
opacity: 0;
font-size: 100px;
}
You can style them, but you can't remove the elements that are already there. If you're creative, you can work with that and do something like this:
input[type=file] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
background: #EEE;
background: linear-gradient(to top, #FFF, #DDD);
border: thin solid rgba(0,0,0, .5);
border-radius: .25em;
box-shadow: inset .25em .25em .25em rgba(255,255,255, .5), inset -.1em -.1em .1em rgba(0,0,0, 0.1);
cursor: text;
padding: .25em;
}
http://jsfiddle.net/zr1x1m2b/1/
I suggest you play around with this code, remove lines, add your own, do whatever until you get something that looks how you like!
to show path of selected file you can try this on html :
<div class="fileinputs">
<input type="file" class="file">
</div>
and in javascript :
var fakeFileUpload = document.createElement('div');
fakeFileUpload.className = 'fakefile';
var image = document.createElement('div');
image.className='fakebtn';
image.innerHTML = 'browse';
fakeFileUpload.appendChild(image);
fakeFileUpload.appendChild(document.createElement('input'));
var x = document.getElementsByTagName('input');
for (var i=0;i<x.length;i++) {
if (x[i].type != 'file') continue;
if (x[i].parentNode.className != 'fileinputs') continue;
x[i].className = 'file hidden';
var clone = fakeFileUpload.cloneNode(true);
x[i].parentNode.appendChild(clone);
x[i].relatedElement = clone.getElementsByTagName('input')[0];
x[i].onchange = x[i].onmouseout = function () {
this.relatedElement.value = this.value;
}
}
and style :
div.fileinputs {
position: relative;
height: 30px;
width: 370px;
}
input.file.hidden {
position: relative;
text-align: right;
-moz-opacity: 0;
filter: alpha(opacity: 0);
opacity: 0;
z-index: 2;
}
div.fakefile {
position: absolute;
top: 0px;
left: 0px;
right: 0;
width: 370px;
padding: 0;
margin: 0;
z-index: 1;
line-height: 90%;
}
div.fakefile input {
margin-bottom: 5px;
margin-left: 0;
border: none;
box-shadow: 0px 0px 2px 1px #ccc;
padding: 4px;
width: 241px;
height: 20px;
}
div.fakefile .fakebtn{
width: 150px;
background: #eb5a41;
z-index: 10;
font-family: roya-bold;
border: none;
padding: 5px 15px;
font-size: 18px;
text-align: center;
cursor: pointer;
-webkit-transition: all 0.4s ease;
-moz-transition: all 0.4s ease;
-o-transition: all 0.4s ease;
-ms-transition: all 0.4s ease;
transition: all 0.4s ease;
display: inline;
margin-left: 3px;
}
div.fileinputs input[type="file"]:hover + div .fakebtn{
background: #DA472E;
}
div.fileinputs input[type="file"] {
opacity: 0;
position: absolute;
top: -6px;
right: 0px;
z-index: 20;
width: 102px;
height: 40px;
cursor: pointer;
}
$(document).ready(function () {_x000D_
$(document).mousemove(function () {_x000D_
$('#myList').css('display', 'block');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$("#hidebtn").css('display', 'none');_x000D_
$('#displayFileNames').html('');_x000D_
$("#myList").html('');_x000D_
var fileArray1 = document.getElementsByClassName('file-input');_x000D_
for (var i = 0; i < fileArray1.length; i++) {_x000D_
var files = fileArray1[i].files;_x000D_
for (var j = 0; j < files.length; j++) {_x000D_
$("#myList").append("<li style='color:black'>" + files[j].name + "</li>");_x000D_
}_x000D_
};_x000D_
_x000D_
if (($("#myList").html()) != '') {_x000D_
$('#unselect').css('display', 'block');_x000D_
$('#divforfile').css('color', 'green');_x000D_
$('#attach').css('color', 'green');_x000D_
$('#displayFileNames').html($("#myList").children().length + ' ' + 'files selezionato');_x000D_
_x000D_
};_x000D_
_x000D_
if (($("#myList").html()) == '') {_x000D_
$('#divforfile').css('color', 'black');_x000D_
$('#attach').css('color', 'black');_x000D_
$('#displayFileNames').append('Nessun File Selezionato');_x000D_
};_x000D_
});_x000D_
_x000D_
});_x000D_
_x000D_
function choosefiles(obj) {_x000D_
$(obj).hide();_x000D_
$('#myList').css('display', 'none');_x000D_
$('#hidebtn').css('display', 'none');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$('#unselect').css('display', 'none');_x000D_
$("#upload-form").append("<input class='file-input inputs' type='file' onclick='choosefiles(this)' name='file[]' multiple='multiple' />");_x000D_
$('#displayFileNames').html('');_x000D_
}_x000D_
_x000D_
$(document).ready(function () {_x000D_
$('#unselect').click(function () {_x000D_
$('#hidebtn').css('display', 'none');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$('#displayFileNames').html('');_x000D_
$("#myList").html('');_x000D_
$('#myFileInput').val('');_x000D_
document.getElementById('upload-form').reset(); _x000D_
$('#unselect').css('display', 'none');_x000D_
$('#divforfile').css('color', 'black');_x000D_
$('#attach').css('color', 'black');_x000D_
_x000D_
});_x000D_
});
_x000D_
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">_x000D_
_x000D_
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">_x000D_
<style>_x000D_
.divs {_x000D_
position: absolute;_x000D_
display: inline-block;_x000D_
background-color: #fff;_x000D_
}_x000D_
_x000D_
.inputs {_x000D_
position: absolute;_x000D_
left: 0px;_x000D_
height: 2%;_x000D_
width: 15%;_x000D_
opacity: 0;_x000D_
background: #00f;_x000D_
z-index: 100;_x000D_
}_x000D_
_x000D_
.icons {_x000D_
position: absolute;_x000D_
}_x000D_
_x000D_
</style>
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<div>_x000D_
<form id='upload-form' action='' method='post' enctype='multipart/form-data'>_x000D_
_x000D_
<div class="divs" id="divforfile" style="color:black">_x000D_
<input id='myFileInput' class='file-input inputs' type='file' name='file[]' onclick="choosefiles(this)" multiple='multiple' />_x000D_
<i class="material-icons" id="attach" style="font-size:21px;color:black">attach_file</i><label>Allegati</label>_x000D_
</div>_x000D_
</form>_x000D_
<br />_x000D_
</div>_x000D_
<br /> _x000D_
<div>_x000D_
<button style="border:none; background-color:white; color:black; display:none" id="seebtn"><p>Files ▼</p></button>_x000D_
<button style="border:none; background-color:white; color:black; display:none" id="hidebtn"><p>Files ▲</p></button>_x000D_
<button type="button" class="close" aria-label="Close" id="unselect" style="display:none;float:left">_x000D_
<span style="color:red">×</span>_x000D_
</button>_x000D_
<div id="displayFileNames">_x000D_
</div>_x000D_
<ul id="myList"></ul>_x000D_
</div>
_x000D_
This is my fully functional customerized file upload/Attachment using jquery & javascript (Visual studio). This will be useful !
Code will be available at the comment section !
Link : https://youtu.be/It38OzMAeig
Enjoy :)
$(document).ready(function () {_x000D_
$(document).mousemove(function () {_x000D_
$('#myList').css('display', 'block');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$("#hidebtn").css('display', 'none');_x000D_
$('#displayFileNames').html('');_x000D_
$("#myList").html('');_x000D_
var fileArray1 = document.getElementsByClassName('file-input');_x000D_
for (var i = 0; i < fileArray1.length; i++) {_x000D_
var files = fileArray1[i].files;_x000D_
for (var j = 0; j < files.length; j++) {_x000D_
$("#myList").append("<li style='color:black'>" + files[j].name + "</li>");_x000D_
}_x000D_
};_x000D_
_x000D_
if (($("#myList").html()) != '') {_x000D_
$('#unselect').css('display', 'block');_x000D_
$('#divforfile').css('color', 'green');_x000D_
$('#attach').css('color', 'green');_x000D_
$('#displayFileNames').html($("#myList").children().length + ' ' + 'files selezionato');_x000D_
_x000D_
};_x000D_
_x000D_
if (($("#myList").html()) == '') {_x000D_
$('#divforfile').css('color', 'black');_x000D_
$('#attach').css('color', 'black');_x000D_
$('#displayFileNames').append('Nessun File Selezionato');_x000D_
};_x000D_
});_x000D_
_x000D_
});_x000D_
_x000D_
function choosefiles(obj) {_x000D_
$(obj).hide();_x000D_
$('#myList').css('display', 'none');_x000D_
$('#hidebtn').css('display', 'none');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$('#unselect').css('display', 'none');_x000D_
$("#upload-form").append("<input class='file-input inputs' type='file' onclick='choosefiles(this)' name='file[]' multiple='multiple' />");_x000D_
$('#displayFileNames').html('');_x000D_
}_x000D_
_x000D_
$(document).ready(function () {_x000D_
$('#unselect').click(function () {_x000D_
$('#hidebtn').css('display', 'none');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$('#displayFileNames').html('');_x000D_
$("#myList").html('');_x000D_
$('#myFileInput').val('');_x000D_
document.getElementById('upload-form').reset(); _x000D_
$('#unselect').css('display', 'none');_x000D_
$('#divforfile').css('color', 'black');_x000D_
$('#attach').css('color', 'black');_x000D_
_x000D_
});_x000D_
});
_x000D_
<style>_x000D_
.divs {_x000D_
position: absolute;_x000D_
display: inline-block;_x000D_
background-color: #fff;_x000D_
}_x000D_
_x000D_
.inputs {_x000D_
position: absolute;_x000D_
left: 0px;_x000D_
height: 2%;_x000D_
width: 15%;_x000D_
opacity: 0;_x000D_
background: #00f;_x000D_
z-index: 100;_x000D_
}_x000D_
_x000D_
.icons {_x000D_
position: absolute;_x000D_
}_x000D_
_x000D_
</style>_x000D_
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
_x000D_
<div>_x000D_
<form id='upload-form' action='' method='post' enctype='multipart/form-data'>_x000D_
_x000D_
<div class="divs" id="divforfile" style="color:black">_x000D_
<input id='myFileInput' class='file-input inputs' type='file' name='file[]' onclick="choosefiles(this)" multiple='multiple' />_x000D_
<i class="material-icons" id="attach" style="font-size:21px;color:black">attach_file</i><label>Allegati</label>_x000D_
</div>_x000D_
</form>_x000D_
<br />_x000D_
</div>_x000D_
<br /> _x000D_
<div>_x000D_
<button style="border:none; background-color:white; color:black; display:none" id="seebtn"><p>Files ▼</p></button>_x000D_
<button style="border:none; background-color:white; color:black; display:none" id="hidebtn"><p>Files ▲</p></button>_x000D_
<button type="button" class="close" aria-label="Close" id="unselect" style="display:none;float:left">_x000D_
<span style="color:red">×</span>_x000D_
</button>_x000D_
<div id="displayFileNames">_x000D_
</div>_x000D_
<ul id="myList"></ul>_x000D_
</div>
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
$(document).ready(function () {_x000D_
$(document).mousemove(function () {_x000D_
$('#myList').css('display', 'block');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$("#hidebtn").css('display', 'none');_x000D_
$('#displayFileNames').html('');_x000D_
$("#myList").html('');_x000D_
var fileArray1 = document.getElementsByClassName('file-input');_x000D_
for (var i = 0; i < fileArray1.length; i++) {_x000D_
var files = fileArray1[i].files;_x000D_
for (var j = 0; j < files.length; j++) {_x000D_
$("#myList").append("<li style='color:black'>" + files[j].name + "</li>");_x000D_
}_x000D_
};_x000D_
_x000D_
if (($("#myList").html()) != '') {_x000D_
$('#unselect').css('display', 'block');_x000D_
$('#divforfile').css('color', 'green');_x000D_
$('#attach').css('color', 'green');_x000D_
$('#displayFileNames').html($("#myList").children().length + ' ' + 'files selezionato');_x000D_
_x000D_
};_x000D_
_x000D_
if (($("#myList").html()) == '') {_x000D_
$('#divforfile').css('color', 'black');_x000D_
$('#attach').css('color', 'black');_x000D_
$('#displayFileNames').append('Nessun File Selezionato');_x000D_
};_x000D_
});_x000D_
_x000D_
});_x000D_
_x000D_
function choosefiles(obj) {_x000D_
$(obj).hide();_x000D_
$('#myList').css('display', 'none');_x000D_
$('#hidebtn').css('display', 'none');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$('#unselect').css('display', 'none');_x000D_
$("#upload-form").append("<input class='file-input inputs' type='file' onclick='choosefiles(this)' name='file[]' multiple='multiple' />");_x000D_
$('#displayFileNames').html('');_x000D_
}_x000D_
_x000D_
$(document).ready(function () {_x000D_
$('#unselect').click(function () {_x000D_
$('#hidebtn').css('display', 'none');_x000D_
$("#seebtn").css('display', 'none');_x000D_
$('#displayFileNames').html('');_x000D_
$("#myList").html('');_x000D_
$('#myFileInput').val('');_x000D_
document.getElementById('upload-form').reset(); _x000D_
$('#unselect').css('display', 'none');_x000D_
$('#divforfile').css('color', 'black');_x000D_
$('#attach').css('color', 'black');_x000D_
_x000D_
});_x000D_
});
_x000D_
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">_x000D_
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">_x000D_
<style>_x000D_
.divs {_x000D_
position: absolute;_x000D_
display: inline-block;_x000D_
background-color: #fff;_x000D_
}_x000D_
_x000D_
.inputs {_x000D_
position: absolute;_x000D_
left: 0px;_x000D_
height: 2%;_x000D_
width: 15%;_x000D_
opacity: 0;_x000D_
background: #00f;_x000D_
z-index: 100;_x000D_
}_x000D_
_x000D_
.icons {_x000D_
position: absolute;_x000D_
}_x000D_
_x000D_
</style>
_x000D_
<div>_x000D_
<form id='upload-form' action='' method='post' enctype='multipart/form-data'>_x000D_
_x000D_
<div class="divs" id="divforfile" style="color:black">_x000D_
<input id='myFileInput' class='file-input inputs' type='file' name='file[]' onclick="choosefiles(this)" multiple='multiple' />_x000D_
<i class="material-icons" id="attach" style="font-size:21px;color:black">attach_file</i><label>Allegati</label>_x000D_
</div>_x000D_
</form>_x000D_
<br />_x000D_
</div>_x000D_
<br /> _x000D_
<div>_x000D_
<button style="border:none; background-color:white; color:black; display:none" id="seebtn"><p>Files ▼</p></button>_x000D_
<button style="border:none; background-color:white; color:black; display:none" id="hidebtn"><p>Files ▲</p></button>_x000D_
<button type="button" class="close" aria-label="Close" id="unselect" style="display:none;float:left">_x000D_
<span style="color:red">×</span>_x000D_
</button>_x000D_
<div id="displayFileNames">_x000D_
</div>_x000D_
<ul id="myList"></ul>_x000D_
</div>
_x000D_
first of all it's a container:
<div class="upload_file_container">
Select file!
<input type="file" name="photo" />
</div>
The second, it's a CSS style, if you want to real more customization, just keeping your eyes is open :)
.upload_file_container{
width:100px;
height:40px;
position:relative;
background(your img);
}
.upload_file_container input{
width:100px;
height:40px;
position:absolute;
left:0;
top:0;
cursor:pointer;
}
This example hasn't style for text inside the button, it depends on font-size, just correct the height and padding-top values for container
Here is one way I recently discovered, with a bit of jQuery
HTML Code:
<form action="">
<input type="file" name="file_upload" style="display:none" id="myFile">
<a onclick="fileUpload()"> Upload a file </a>
</form>
For the javascript/jQuery part :
<script>
function fileUpload() {
$("#myFile").click();
}
</script>
In this example, I have put an "anchor" tag to trigger the file upload. You can replace with anything you want, just remember to put the "onclick" attribute with the proper function.
Hope this helps!
P.S. : Do not forget to include jQuery from CDN or any other source
It's much better if you just use a <label>
, hide the <input>
, and customize the label.
HTML:
<input type="file" id="input">
<label for="input" id="label">Choose File</label>
CSS:
input#input{
display: none;
}
label#label{
/* Customize your label here */
}
I went for this option which clarifies how to fully customize the browse button by including an handler of the uploaded file name, also customized. It adds additional fields and client-side controls on them just to show how to include the browse in a "real" form, not just a standalone.
Here's the codepen: http://codepen.io/emiemi/pen/zxNXWR
JS:
//click on our custom btn triggers a click on the hidden actual file input
$("#btnup").click(function(){
$("#fileup").click();
});
//changes on the three fields (input, tit,and name) trigger a control which checks if the 3 fields are all filled and if file field is valid (an image is uploaded)
$('#fileup').change(function(){
var formDOMObj = document.upload;
//here we assign tu our text field #fileup the name of the selected file
var res=$('#fileup').val();
var arr = res.split("\\");
//if file is not valid we show the error icon and the red alert
if (formDOMObj.fileup.value.indexOf(".jpg") == -1 && formDOMObj.fileup.value.indexOf(".png") == -1 && formDOMObj.fileup.value.indexOf(".jpeg") == -1 && formDOMObj.fileup.value.indexOf(".bmp") == -1 && formDOMObj.fileup.value.indexOf(".JPG") == -1 && formDOMObj.fileup.value.indexOf(".PNG") == -1 && formDOMObj.fileup.value.indexOf(".JPEG") == -1 && formDOMObj.fileup.value.indexOf(".BMP") == -1){
$( ".imgupload" ).hide("slow");
$( ".imguploadok" ).hide("slow");
$( ".imguploadstop" ).show("slow");
$('#nomefile').css({"color":"red","font-weight":700});
$('#nomefile').html("The file "+arr.slice(-1)[0]+" is not an image!");
$( "#bottone" ).hide();
$( "#fakebtn" ).show();
}else{
//if file is valid we show the green alert
$( ".imgupload" ).hide("slow");
$( ".imguploadstop" ).hide("slow");
$( ".imguploadok" ).show("slow");
$('#nomefile').html(arr.slice(-1)[0]);
$('#nomefile').css({"color":"green","font-weight":700});
if (formDOMObj.nome.value!=""&&formDOMObj.tit.value!=""&&formDOMObj.fileup.value!=""){
//if all three fields are valid the fake input btn is hidden and the actual one i s finally hown
$( "#fakebtn" ).hide();
$( "#bottone" ).show();
}
}
});
$('#nome').change(function(){
//same as file change but on name field
var formDOMObj = document.upload;
if (formDOMObj.nome.value!=""&&formDOMObj.tit.value!=""&&formDOMObj.fileup.value!=""){
$( "#fakebtn" ).hide();
$( "#bottone" ).show();
}else{
$( "#bottone" ).hide();
$( "#fakebtn" ).show();
}
});
$('#tit').change(function(){
//same as file change but on tit field
var formDOMObj = document.upload;
if (formDOMObj.nome.value!=""&&formDOMObj.tit.value!=""&&formDOMObj.fileup.value!=""){
$( "#fakebtn" ).hide();
$( "#bottone" ).show();
}else{
$( "#bottone" ).hide();
$( "#fakebtn" ).show();
}
});
HTML:
<form name="upload" method="post" action="/" enctype="multipart/form-data" accept-charset="utf-8">
<div class="row">
<div class="col-md-6 center">
<!--this is the actual file input, s hidden beacause we wanna use our custom one-->
<input type="file" value="" class="hidden" name="fileup" id="fileup">
<div class="btn-container">
<!--the three icons: default, ok file (img), error file (not an img)-->
<h1 class="imgupload"><i class="fa fa-file-image-o"></i></h1>
<h1 class="imguploadok"><i class="fa fa-check"></i></h1>
<h1 class="imguploadstop"><i class="fa fa-times"></i></h1>
<!--this field changes dinamically displaying the filename we are trying to upload-->
<p id="nomefile">Only pics allowed! (jpg,jpeg,bmp,png)</p>
<!--our custom btn which triggers the actual hidden one-->
<button type="button" id="btnup" class="btn btn-primary btn-lg">Browse for your pic!</button>
</div>
</div>
<!--additional fields-->
<div class="col-md-6">
<div class="row">
<div class="form-group" id="top">
<div class="col-md-12">
<input type="text" maxlength="100" class="form-control" name="nome" id="nome" placeholder="Your Name">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<input type="text" maxlength="50" class="form-control" name="tit" id="tit" placeholder="I am rubber, you are glue">
</div>
</div>
</div>
<div class="row">
<div class="col-md-8">
<p class="white">All fields are mandatory</p>
</div>
<div class="col-md-4">
<!--the defauld disabled btn and the actual one shown only if the three fields are valid-->
<input type="submit" value="Submit!" class="btn btn-primary" id="bottone" style="padding-left:50px; padding-right:50px; display:none;">
<button type="button" class="btn btn-default" disabled="disabled" id="fakebtn" style="padding-left:40px; padding-right:40px;">Submit! <i class="fa fa-minus-circle"></i></button>
</div>
</div>
</div>
</div>
<label className="btn btn-info btn-lg">
Upload
<input type="file" style="display: none" />
</label>
If you're using bootstrap here is a better solution :
<label class="btn btn-default btn-file">
Browse <input type="file" style="display: none;" required>
</label>
For IE8 and below http://www.abeautifulsite.net/whipping-file-inputs-into-shape-with-bootstrap-3/
Easiest way..
<label>
Upload
<input type="file" style="visibility: hidden;"/>
</label>
Just style a normal button however you want, using your favorite CSS.
Then call a simple JS function to create and link a hidden input element to your styled button. Don't add browser-specific CSS to do the hiding part.
<!DOCTYPE html>
<meta charset="utf-8">
<style>
button {
width : 160px;
height : 30px;
font-size : 13px;
border : none;
text-align : center;
background-color : #444;
color : #6f0;
}
button:active {
background-color : #779;
}
</style>
<button id="upload">Styled upload button!</button>
<script>
function Upload_On_Click(id, handler) {
var hidden_input = null;
document.getElementById(id).onclick = function() {hidden_input.click();}
function setup_hidden_input() {
hidden_input && hidden_input.parentNode.removeChild(hidden_input);
hidden_input = document.createElement("input");
hidden_input.setAttribute("type", "file");
hidden_input.style.visibility = "hidden";
document.querySelector("body").appendChild(hidden_input);
hidden_input.onchange = function() {
handler(hidden_input.files[0]);
setup_hidden_input();
};
}
setup_hidden_input();
}
Upload_On_Click("upload", function(file) {
console.log("GOT FILE: " + file.name);
});
</script>
Notice how the above code re-links it after every time the user chooses a file. This is important because "onchange" is only called if the user changes the filename. But you probably want to get the file every time the user provides it.
For more details, research DropZone and gmail uploads.
Source: Stackoverflow.com