Skip to content

Instantly share code, notes, and snippets.

@devfaysal
Created November 13, 2018 06:12
Show Gist options
  • Save devfaysal/c01a1691b49f40bde26eefd97d66cb1e to your computer and use it in GitHub Desktop.
Save devfaysal/c01a1691b49f40bde26eefd97d66cb1e to your computer and use it in GitHub Desktop.
Preview Image while uploading
//<input onchange="previewFile('#logo_preview', '#logo')" id="logo" type="file" name="logo">
//<img id="logo_preview" src="">
function previewFile(preview, source) {
var preview = document.querySelector(preview);
var file = document.querySelector(source).files[0];
var reader = new FileReader();
reader.onloadend = function () {
preview.src = reader.result;
}
if (file) {
reader.readAsDataURL(file);
} else {
preview.src = "";
}
console.log(preview.src);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment