How to get Base64Image from input type file?

<input  type="file" id="user_img_input" onchange="upload_user_img(this);" /> 
<img src="" id="img" />
<script>
function setBase64Image(object, img_id) {
    if ( object.files && object.files[0] ) {
        var FR = new FileReader();
        FR.onload = function(e) {

             $(img_id).attr("src", e.target.result);
        };       
        FR.readAsDataURL( object.files[0] );
    }
}

 function upload_user_img(inputObject)
  {
    setBase64Image(inputObject, '#img');
  }
</script>

Live Demo:

Leave a Reply

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