How to Preview Image Before Upload Using Javascript

How to Preview Image Before Upload Using Javascript

Welcome folks I am back with another blog post. In this post we will be implementing How to Preview Image Before Upload using Javascript from scratch. Image Preview is one of the most important features when it comes to image uploading. In this post we will be implementing this feature in the following steps.

READ  What is the Difference Between Node and Nodemon


Make a new index.html file and copy paste the below code. In this code we have just a file upload button.


<!DOCTYPE html>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Image Preview Example in Javascript</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" type="text/css" media="screen" href="main.css">
    <script src="preview.js"></script>
    <div id="wrapper">
        <input type="file" accept="image/*" onchange="previewImage(event)">
        <img id="outputimage"/>


READ  Build a Webcam Filter Picture App with Webrtc & Canvas

Make a new preview.css file for the stylesheet. It will style the application.


 margin:0 auto;
 margin:0 auto;


READ  PDF Generator in Javascript - JSPDF Tutorial

Make a new preview.js file. This file will hold the logic for the preview image which will be displayed to the user before image upload.


function preview_image(event) 
 var reader = new FileReader();
 reader.onload = function()
  var output = document.getElementById('output_image');
  output.src = reader.result;


Leave a Reply

Close Menu