Want To Be A Blogger  →  Start A Blog  →  Increase Traffic  →  Make Money Online  →  Recommended Things

Preview Image Before Upload Using JavaScript

Friday, Jan 5, 2018

Tags:- JavaScript HTML CSS

Image preview is a great feature for user to check there image before upload whether the correct image is going to upload or if the image looks nice or not etc.So in this tutorial we will show you how to preview image before upload using JavaScript,HTML and CSS.You may also like upload multiple images with preview using jQuery and PHP.

See Demo
Preview Image Before Upload Using JavaScript

To Preview Image Before Upload It Takes Only One Step:-

  1. Make a HTML file and define markup,scripting and styling

Step 1.Make a HTML file and define markup,scripting and styling

We make a HTML file and save it with a name preview.html

<script type='text/javascript'>
function preview_image(event) 
 var reader = new FileReader();
 reader.onload = function()
  var output = document.getElementById('output_image');
  output.src = reader.result;
 margin:0 auto;
 margin:0 auto;
<div id="wrapper">
 <input type="file" accept="image/*" onchange="preview_image(event)">
 <img id="output_image"/>

In this step we create a file tag that accepts only images and it calls preview_image() function with the change event as a parameter when user choose image from folder.In preview() function we first create FileReader object to get the file data and then when the reader is loaded we create a function to get the output image and change its source to result and then we use readAsDataURL to preview the image.You may also like jQuery live preview.

Thats all, this is how to preview image before upload using JavaScript, CSS and HTML.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Subscribe Our Newsletter And Get Tutorials And Offers Via Email

About Me

Hello, My Name is Mudit Jain. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of TalkersCode. I started this blog in february 2015 to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers.