Sunday, December 14, 2014

Get input file image in base64 encoded using jQuery.

<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.11.1.min.js"></script>

<style type="text/css">

#base{
  padding:10px;
  background:#eee;
}

</style>

</head>
<body>


<input type='file' id="asd" />
<img id="img" src="" />
<div id="base"></div>

<script type="text/javascript">

function readImage(input) {
    if ( input.files && input.files[0] ) {
        var FR= new FileReader();
        FR.onload = function(e) {
             $('#img').attr( "src", e.target.result );
             $('#base').text( e.target.result );
        };    
        FR.readAsDataURL( input.files[0] );
    }
}

$("#asd").change(function(){
    readImage( this );
});

</script>

</body>
</html>


Output :)

1)

2)

No comments:

Post a Comment