Image previews using base64 – Javascript

var fileUpload = document.querySelector('input[type=file]');
var file    = fileUpload.files[0];
var preview = $('.uploaded_file') // img tag
var reader  = new FileReader();
reader.readAsDataURL(file);
reader.onload = function() {
   preview.src = reader.result // it will set the base64 src to img tag
}

// Be-Aware of browser supports (https://developer.mozilla.org/en/docs/Web/API/FileReader)
Advertisements