Join TalkersCode Now !

Get Latest Tutorials And Links On Web Development
And Get Existing Offers Via Email

Preview Image Before Upload Using JavaScript

Saturday, Apr 8, 2017

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.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email

Subscribe TalkersCode! One Of The Best
Web Development And Review Blog

Get Latest Tutorials And Reviews
Via Email