JSFiddle - React, Tailwind, and code Playground
HTML
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.css">
<div class="preview-image-container" style="cursor: default;">
<input type="file" name="File" id="File" class="image-url form-control" accept="image/*" style="display:none;">
<div class="preview-image-dummy"></div><img class="preview-image-url">
<div class="preview-image-instruction" style="background-color: inherit; cursor: default;"><i class="fa fa-4x fa-cloud-upload" aria-hidden="true"></i>
<div class="preview-image-btn-browse btn btn-primary">Browse</div>
<p>or drop a file here.</p>
</div>
</div>
CSS
.preview-image-container {
display: inline-block;
position: relative;
width: 100%;
max-width: 100%;
border: 2px dotted #bbb;
border-radius: 5px;
}
.preview-image-dummy {
margin-top: 75%;
}
.preview-image-url {
display: block;
margin: auto;
max-width: 100%;
max-height: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.preview-image-container .close {
position: absolute;
top: 2px;
right: 2px;
z-index: 2;
background-color: #FFF;
padding: 5px 2px 2px;
color: #000;
font-weight: bold;
cursor: pointer;
opacity: .2;
text-align: center;
font-size: 22px;
line-height: 10px;
border-radius: 50%;
}
.preview-image-container:hover .close {
opacity: 1;
}
.preview-image-instruction {
text-align: center;
display: block;
margin: auto;
max-width: 100%;
max-height: 100%;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.preview-image-instruction > i {
margin-top: 15%;
}
.preview-image-btn-browse {
display: table;
margin: auto;
}
JavaScript
function readURL(input) {
if (input.files && input.files[0]) {
ShowThumbnail(input.files[0], input);
}
}
$("input.image-url:file").change(function() {
readURL(this);
var i = $("input.image-url:file").files.length;
console.log(i);
});
$(".preview-image-container").on('dragover', dragover_handler);
$(".preview-image-container").on('dragleave', dragleave_handler);
$(".preview-image-container").on('drop', drop_handler);
$(".image-url-delete").on('click', delete_handler);
$('.preview-image-btn-browse').click(function() {
$(this).closest('.preview-image-container').find('.image-url').trigger('click');
});
function dragleave_handler(ev) {
$(ev.target).css("backgroundColor", "");
$(ev.target).css("cursor", "default");
}
function dragover_handler(ev) {
// Prevent default select and drag behavior
ev.preventDefault();
var dt = ev.originalEvent.dataTransfer;
dt.dropEffect = "move";
if (dt.types && (dt.types.indexOf ? dt.types.indexOf('Files') != -1 : dt.types.contains('Files'))) {
$(ev.target).css("backgroundColor", "#87CEFA");
$(ev.target).css("cursor", "pointer");
}
}
function drop_handler(ev) {
ev.preventDefault();
var target = $(ev.target);
target.css("backgroundColor", "inherit");
target.css("cursor", "default");
HideInstruction(target);
// If dropped items aren't files, reject them
var dt = ev.originalEvent.dataTransfer;
if (dt.items && dt.items[0]) {
// Use DataTransferItemList interface to access the file(s)
if (dt.items[0].kind == "file") {
var f = dt.items[0].getAsFile();
ShowThumbnail(f, target);
}
} else {
// Use DataTransfer interface to access the file(s)
if (dt.files && dt.files[0]) {
ShowThumbnail(dt.files[0], target);
}
}
}
function ShowThumbnail(file, previewDOM) {
var reader = new FileReader();
reader.onload = function(e) {
var imagePreview = $(previewDOM).siblings('.preview-image-url');
imagePreview.attr('src', e.target.result);
...