
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Preview an image before it is uploaded in JavaScript
For security reasons browsers do not allow accessing the path of the image file selected via an input, i.e.JavaScript in browsers has no access to the File System. Therefore, our task is to preview the image file selected via the input before we send it to any server or anywhere else.
Method 1: Using the URL class:
WE can use the createObjectURL() function of the URL class to create a url of the image selected by the input and then provide that url to the src attribute of an img tag.
Example
The code for this will be −
<img id="preview"/> <input type="file" accept="image/*" onchange="previewImage(event)"> <script> const previewImage = e => { const preview = document.getElementById('preview'); preview.src = URL.createObjectURL(e.target.files[0]); preview.onload = () => URL.revokeObjectURL(preview.src); }; </script>
Method 2: Using the FileReader class:
This method will parse the file taken in by the <input /> and then it will create a string containing a base64 representation of the image.
Example
The code for this will be −
<img id="preview"/> <input type="file" accept="image/*" onchange="previewImage(event)"> <script> const previewImage = e => { const reader = new FileReader(); reader.readAsDataURL(e.target.files[0]); reader.onload = () => { const preview = document.getElementById('preview'); preview.src = reader.result; }; }; </script>
Output
The output for both the methods will look like −
- Related Articles
- How to preview an image before it is uploaded in JavaScript?
- How to preview an image before and after upload in HTML and JavaScript?
- Can I use a JavaScript variable before it is declared?
- Drawing an image in canvas using in JavaScript
- Preview into eCommerce Careers
- Resize image before submitting the form HTML5
- How to create an image map in JavaScript?
- Insert a node as a child before an existing child in JavaScript?
- Where will be the uploaded files stored in JSP?
- Different options in Data Preview in SAP HANA
- Why it is not possible to capture an image formed by convex mirror?
- What is an image?
- How to add image before optgroup label using Bootstrap?
- How to read an input image and print it into an array in matplotlib?
- An object of height 1.2m is placed before a concave mirror of focal length 20cm so that a real image is formed at a distance of 60cm from it. Find the position of an object. What will be the height of the image formed?

Advertisements