A Pen by Patrick Thompson on CodePen.
Last active
March 17, 2017 10:40
-
-
Save patrickt010/a7fff46587302db77668 to your computer and use it in GitHub Desktop.
Bootstrap File Picker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src="//netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> | |
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet"> | |
<div class="container" style="margin-top: 20px;"> | |
<div class="row"> | |
<div> | |
<h4>Documents</h4> | |
<div class="input-group"> | |
<span class="input-group-btn"> | |
<span class="btn btn-primary btn-file"> | |
Browse… <input type="file" multiple> | |
</span> | |
</span> | |
<input type="text" class="form-control" readonly> | |
</div> | |
</div> | |
</div> | |
</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).on('change', '.btn-file :file', function() { | |
var input = $(this), | |
numFiles = input.get(0).files ? input.get(0).files.length : 1, | |
label = input.val().replace(/\\/g, '/').replace(/.*\//, ''); | |
input.trigger('fileselect', [numFiles, label]); | |
}); | |
$(document).ready( function() { | |
$('.btn-file :file').on('fileselect', function(event, numFiles, label) { | |
var input = $(this).parents('.input-group').find(':text'), | |
log = numFiles > 1 ? numFiles + ' files selected' : label; | |
if( input.length ) { | |
input.val(log); | |
} else { | |
if( log ) alert(log); | |
} | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.btn-file { | |
position: relative; | |
overflow: hidden; | |
} | |
.btn-file input[type=file] { | |
position: absolute; | |
top: 0; | |
right: 0; | |
min-width: 100%; | |
min-height: 100%; | |
font-size: 100px; | |
text-align: right; | |
filter: alpha(opacity=0); | |
opacity: 0; | |
background: red; | |
cursor: inherit; | |
display: block; | |
} | |
input[readonly] { | |
background-color: white !important; | |
cursor: text !important; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment