Skip to main content
Login Join
Snippet · HTML

File Upload with Image Preview

Shared by Mukesh Gujjar · July 20, 2026

3 views
Back to Snippets

Create a file upload input that displays a preview of the selected image before upload.

Usage

Ideal for:

<input
type="file"
id="image"
accept="image/*">

<br><br>

<img
id="preview"
width="250">

<script>

document
.getElementById('image')
.addEventListener('change',function(){

const file=this.files[0];

if(file){

document.getElementById('preview').src=
URL.createObjectURL(file);

}

});

</script>
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments