It’s an interesting feature. In some cases it’s better when users can preview image before uploading it, e.g. when you want to let users trim the image or process it in any way. As you’ll see it’s pretty easy to do. After reading this article you will know how to add this feature to your web app using Javascript.
Step by step guide to creating preview image
We’ll start off with a simple HTML page with a form and and input element which accepts only image files. Notice that we have an img
element with empty src
attribute, which we’ll use to display selected image.
|
|
Firstly, we’ll update the preview after user picks image. We will do that by handling onchange
event which is triggered once user selects the image. In order to do that we create a new <script>
section with an empty function. Later on we’ll implement it to create preview. Notice that we’re passing element as a parameter, which in this case should be our input
element.
|
|
Then we hook it up with onchange
event. Note that we’re passing input element as a parameter using this
:
|
|
The next step it so retrieve selected file using the files
array which can be accessed from the element. To make it simple I’ve assumed that there is only one file.
|
|
To read the file we’ll use <a href="https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL">FileReader.readAsDataURL</a>
, which will convert image into data url.
Data urls allows to embedd files inline in elements, typically using base64 encoding. For example you could display black 1px by 1px image using this code (generated using Png Pixel):
|
|
Then we will use the result (base64 encoded data url image) as an preview image. In this case we have to wait until readAsDataURL
is done to get the result, which happens inside load event listener.
|
|
This will display the image, in full size, so you may want to consider styling it and constraining width and height :)