Skip to content

Instantly share code, notes, and snippets.

@rustyvz
Last active April 7, 2025 19:44
Show Gist options
  • Save rustyvz/31606a38b9334f0178fc22d341d4dff9 to your computer and use it in GitHub Desktop.
Save rustyvz/31606a38b9334f0178fc22d341d4dff9 to your computer and use it in GitHub Desktop.
Display Uploaded Images
<!-- Use the FileReader API's readAsDataURL method to show uploaded images -->
<input type="file" id="uploaded-file" />
<div id="result"></div>
<script>
function readImage() {
const fileReader = new FileReader();
const file = document.getElementById("uploaded-file").files[0];
if (file) {
fileReader.readAsDataURL(file);
}
fileReader.addEventListener(
"load",
() => {
const img = document.createElement("img");
img.src = fileReader.result;
document.getElementById("result").append(img);
},
{ once: true }
);
}
document.getElementById("uploaded-file").addEventListener("change", readImage);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment