2 Answers 272996 Views
Displaying image in HTML form field on file upload is very easy with the use of javascript. Let's add a little more effect to make it look more attractive.
If you have a look at most of the social platforms, they have one thing in common related to file upload and that's the file or image upload button which is not present in the form and a plain text or icon link does the work. It helps us get rid of the boring file upload button from our HTML page while getting the job of file upload done. Let's see, how its's done.
<input type="file" accept="image/*" name="image" id="file" style="display: none;">
If you want to allow upload of all image types in your form, then you can leave it as shown above. You can replace the properties on the attribute accept if you want specify the image extensions to be uploaded via your form as shown below.
<input type="file" accept="image/gif, image/jpeg, image/png" name="image" id="file" style="display: none;">
We need a name for the input field to get it added to the database, so we provided it with a name image. Provide an id to your file input form and add style display: none or visibility:hidden. Display property is better to use here as it won't occupy the space in your page.
Now, create a label with the same id added in for attribute to connect it to the file input field as shown below and you are done.
<p><label for="file" style="cursor: pointer;">Upload Image</label></p>
Now, if you click on the label, the file explorer will get opened as it would do on click of the button of file input.
But, once you hide those file upload buttons from your HTML page, the file information displayed by the side of the button on file upload gets hidden too which might make the users confused on whether the image is uploaded or not. In such cases, we can display the uploaded images for their convenience as well as improve the outlook of the form page too.
So, let's add an HTML <img> element to display the uploaded image.
<img id="output" width="200" />
Here, we added an HTML <img> tag with no source but an id which will retrieve the image source from javascript and defined it's with to be 200px.
In order to activate an event defined in any javascript code, we'll need a event handler in your HTML element, so we'll add that to our file input field.
<input type="file" accept="image/*" name="image" id="file" onchange="loadFile(event)" style="display: none;">
Here, loadFile(event) is our event handler that'll be activated once the value in the input field is changed so the chage in that input field is our event.
Let's add the javascript code to display the image in the HTML element <img id="output"> then.
<script> var loadFile = function(event) { var image = document.getElementById('output'); image.src = URL.createObjectURL(event.target.files[0]); }; </script>
Here, our event handler will hold a value which will be retrieved through the image file being uploaded via the input file button as an URL which we require to display the image in our webpage.
<p><input type="file" accept="image/*" name="image" id="file" onchange="loadFile(event)" style="display: none;"></p> <p><label for="file" style="cursor: pointer;">Upload Image</label></p> <p><img id="output" width="200" /></p> <script> var loadFile = function(event) { var image = document.getElementById('output'); image.src = URL.createObjectURL(event.target.files[0]); }; </script>
In this way, you can display the uploaded file in html using javascript in an attractive way.
145 Likes 0 Dislike 5 Comments
27 Oct, 2021
can we do this for input that accepts multiple inputs as well?
27 Mar, 2022
Thank you this is exactly what I was looking for!
29 Aug, 2022
Excellent ...
17 Sep, 2022
This is the best. Amazing.
02 May, 2023
hello there! this was immensely useful but the result i want to achieve is, How do you make the image appear on the submitted form, i'd be really glad to find out a way to make image display on the submitted form when you open it in email, if thats possible. Thanks
07 May, 2023
You'll need to store the image in your server and then send the link of the image to the email along with other form data. I hope you know how to store the image.
thx bro.. thats what i was looking for... simple and clear.
function readURL(event) {
var activimg = document.getElementsByClassName("activimg");
var url = URL.createObjectURL(event.target.files[0]);
activimg[0].innerHTML="<img class=\"output\" src=\"" + url + "\" alt=\"img\" >";
}
Jiwan Thapa
08 Nov, 2021
Please use this for multiple uploads. <p><input type="file" accept="image/*" name="multiplephoto[]" id="multipleupload" multiple style="display: none;"></p> <div id="preview"></div> <p><label for="multipleupload" class="btn btn-warning">Upload Photos</label></p> $(function () { $("#multipleupload").change(function () { if (typeof (FileReader) != "undefined") { var dvPreview = $("#preview"); dvPreview.html(""); var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.jpg|.jpeg|.gif|.png|.bmp)$/; $($(this)[0].files).each(function () { var file = $(this); if (regex.test(file[0].name.toLowerCase())) { var reader = new FileReader(); reader.onload = function (e) { var img = $("<img />"); img.attr("style", "width:300px;margin-right:10px;border:1px solid #f7f7f7;padding:5px;background:#fff;margin-bottom:10px;"); img.attr("src", e.target.result); dvPreview.append(img); } reader.readAsDataURL(file[0]); } }); } }); });