S Image Preview Before Upload In jQuery Bootstrap Example

S Image Preview Before Upload In jQuery Bootstrap Example

In this post we will give you information about S Image Preview Before Upload In jQuery Bootstrap Example. Hear we will give you detail about S Image Preview Before Upload In jQuery Bootstrap ExampleAnd how to use it also give you demo for it if it is necessary.

Today, Laravel share one helpfull tutorials about jquery and bootstrap. in this tutorials we are show you how to show image preview before upload.


We are need some time this type functionality in frontend site which you want to show image preview before it upload in server. resently we are working on one laravel application and we are need this so we are make one simple script for it using jquery and bootstrap. and we are finaly done this with jquery and bootstrap.


Here, we are always share our and example which we are facing in our devloping mode and when we are find finaly solution about it the we are also share with you.


You can try this example with bootstrap because we are using bootstrap for fine layout.



Step : 1 HTML look like




<!DOCTYPE html>
<html>
<head>
	<title>Show Image Preview Before Upload</title>
	<link rel="stylesheet" type="text/css" href="#">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7https://onlinecode.org/js/bootstrap.min.js"></script>
</head>
<body>
	<div >
	    <div >    
	        <div >  
	            <div >
	                <span >
	                    <button type="button"  style="display:none;">
	                        <span ></span> Clear
	                    </button>
	                    <div >
	                        <span ></span>
	                        <span >Browse</span>
	                        <input type="file" accept="image/png, image/jpeg, image/gif" name="input-file-preview"/>
	                    </div>
	                </span>
	            </div>
	        </div>
	    </div>
	</div>
</body>
</html>



Step : 2 CSS look like


Then add some css copy following and add in your css file.




.image-preview-input {
    position: relative;
	overflow: hidden;
	margin: 0px;    
    color: #333;
    background-color: #fff;
    border-color: #ccc;    
}
.image-preview-input input[type=file] {
	position: absolute;
	top: 0;
	right: 0;
	margin: 0;
	padding: 0;
	font-size: 20px;
	cursor: pointer;
	opacity: 0;
	filter: alpha(opacity=0);
}
.image-preview-input-title {
    margin-left:2px;
}	



Step : 3 jQuery look like



The finaly add following jQuery in js file.




$(document).on('click', '#close-preview', function(){ 
    $('.image-preview').popover('hide');
    // Hover befor close the preview
    $('.image-preview').hover(
        function () {
           $('.image-preview').popover('show');
        }, 
         function () {
           $('.image-preview').popover('hide');
        }
    );    
});

$(function() {
    // Create the close button
    var closebtn = $('<button/>', {
        type:"button",
        text: 'x',
        id: 'close-preview',
        style: 'font-size: initial;',
    });
    closebtn.attr("class","close pull-right");
    // Set the popover default content
    $('.image-preview').popover({
        trigger:'manual',
        html:true,
        title: "<strong>Preview</strong>"+$(closebtn)[0].outerHTML,
        content: "There's no image",
        placement:'bottom'
    });
    // Clear event
    $('.image-preview-clear').click(function(){
        $('.image-preview').attr("data-content","").popover('hide');
        $('.image-preview-filename').val("");
        $('.image-preview-clear').hide();
        $('.image-preview-input input:file').val("");
        $(".image-preview-input-title").text("Browse"); 
    }); 
    // Create the preview image
    $(".image-preview-input input:file").change(function (){     
        var img = $('<img/>', {
            id: 'dynamic',
            width:250,
            height:200
        });      
        var file = this.files[0];
        var reader = new FileReader();
        // Set preview image into the popover data-content
        reader.onload = function (e) {
            $(".image-preview-input-title").text("Change");
            $(".image-preview-clear").show();
            $(".image-preview-filename").val(file.name);            
            img.attr('src', e.target.result);
            $(".image-preview").attr("data-content",$(img)[0].outerHTML).popover("show");
        }        
        reader.readAsDataURL(file);
    });  
});



Now we are ready to run our example so run bellow command ro quick run:


If you face any problem then please write a comment or give some suggestions for improvement. Thanks…

Hope this and post will helped you for implement S Image Preview Before Upload In jQuery Bootstrap Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

3  +  5  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US