JQuery & Bootstrap image upload with advance functionality
Created
February 18, 2022 15:14
-
-
Save tornikeo/e4c9e8ce6f6cfb2c5139e09b8d712f1b to your computer and use it in GitHub Desktop.
Bootstrap image upload preview plugin
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
<br><div class="container"> | |
<div class="row"> | |
<div class="col-sm-2 imgUp"> | |
<div class="imagePreview"></div> | |
<label class="btn btn-primary"> | |
Upload<input type="file" class="uploadFile img" value="Upload Photo" style="width: 0px;height: 0px;overflow: hidden;"> | |
</label> | |
</div><!-- col-2 --> | |
<i class="fa fa-plus imgAdd"></i> | |
</div><!-- row --> | |
</div><!-- container --> | |
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
$(".imgAdd").click(function(){ | |
$(this).closest(".row").find('.imgAdd').before('<div class="col-sm-2 imgUp"><div class="imagePreview"></div><label class="btn btn-primary">Upload<input type="file" class="uploadFile img" value="Upload Photo" style="width:0px;height:0px;overflow:hidden;"></label><i class="fa fa-times del"></i></div>'); | |
}); | |
$(document).on("click", "i.del" , function() { | |
$(this).parent().remove(); | |
}); | |
$(function() { | |
$(document).on("change",".uploadFile", function() | |
{ | |
var uploadFile = $(this); | |
var files = !!this.files ? this.files : []; | |
if (!files.length || !window.FileReader) return; // no file selected, or no FileReader support | |
if (/^image/.test( files[0].type)){ // only image file | |
var reader = new FileReader(); // instance of the FileReader | |
reader.readAsDataURL(files[0]); // read the local file | |
reader.onloadend = function(){ // set image data as background of div | |
//alert(uploadFile.closest(".upimage").find('.imagePreview').length); | |
uploadFile.closest(".imgUp").find('.imagePreview').css("background-image", "url("+this.result+")"); | |
} | |
} | |
}); | |
}); |
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="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> | |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> |
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
body | |
{ | |
background-color:#f5f5f5; | |
} | |
.imagePreview { | |
width: 100%; | |
height: 180px; | |
background-position: center center; | |
background:url(http://cliquecities.com/assets/no-image-e3699ae23f866f6cbdf8ba2443ee5c4e.jpg); | |
background-color:#fff; | |
background-size: cover; | |
background-repeat:no-repeat; | |
display: inline-block; | |
box-shadow:0px -3px 6px 2px rgba(0,0,0,0.2); | |
} | |
.btn-primary | |
{ | |
display:block; | |
border-radius:0px; | |
box-shadow:0px 4px 6px 2px rgba(0,0,0,0.2); | |
margin-top:-5px; | |
} | |
.imgUp | |
{ | |
margin-bottom:15px; | |
} | |
.del | |
{ | |
position:absolute; | |
top:0px; | |
right:15px; | |
width:30px; | |
height:30px; | |
text-align:center; | |
line-height:30px; | |
background-color:rgba(255,255,255,0.6); | |
cursor:pointer; | |
} | |
.imgAdd | |
{ | |
width:30px; | |
height:30px; | |
border-radius:50%; | |
background-color:#4bd7ef; | |
color:#fff; | |
box-shadow:0px 0px 2px 1px rgba(0,0,0,0.2); | |
text-align:center; | |
line-height:30px; | |
margin-top:0px; | |
cursor:pointer; | |
font-size:15px; | |
} |
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
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" /> | |
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment