Image preview is a great feature for user to check their 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 an image before upload using pure Vanilla JavaScript, HTML and CSS. You may also like to upload multiple images with preview using jQuery and PHP.
This is a simple frontend utility to help the file-upload process on your website. It is written in pure JavaScript, has no dependencies. You can check out the live demo. For the most part, browsers do a good job of handling image uploads. That being said – we find that the ability to show our users a preview of their upload can go a long way in increasing the confidence in their upload.
Table of Contents
Features:
- Previe Image In A DIV Before Uploading.
- Generate Thumbnail On File Select.
- Light Weight.
- Pure JavaScrit.
- Cross Browser.
- Your code will be lightweight because it will have zero dependencies.
- Your website will perform better. You won’t need to load external scripts and stylesheets.
- You’ll have more control. You will have built the clock to behave exactly the way you want it to (rather than trying to bend a plugin to your will).
How To Preview Image Before Uploading In <input=”file”> Field Using Pure JavaScript?
There are few easy and understandable steps to achieve your desired functionality using pure Vanilla JavaScript that we are gonna share below. Follow each step perfectly.
HTML:
<input type="file" name="FileUpload" accept="image/*" id="ImgFileUpload"/> <img id="previewImageFile" />
JavaScript:
<script type="text/javascript"> /* Preview Image Before Uploading ----------------------------------------------- */ ImgFileUpload.onchange = evt => { const [file] = ImgFileUpload.files if (file) { previewImageFile.src = URL.createObjectURL(file) } }</script>
Customization:
No need to customize it. Just copy paste.
Troubleshooting the Errors
Do it with concentration and patience. Check your alls steps and again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.
Final Words:
That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us or If you have any doubts and problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave your precious feedback in our comment form below. Happy development, See you in the next article.
Be the first to write a comment.