I have a page with four images for the user to select. I want the user to be able to preview each image on the site before upload.
The JavaScript code below works for only one image but I would like it to work for multiple images uploaded via <input type="file">
.
What will be the best way to do this?
function readURL(input) { if (input.files && input.files[0]) { var reader = new FileReader(); reader.onload = function (e) { $('#output').attr('src', e.target.result); } reader.readAsDataURL(input.files[0]); } } $("#file-input").change(function () { readURL(this); });
We have two main elements that we will interact with using JavaScript. First, we have a division element that contains the “img” tag. Using Jquery, we will change the “src” attribute of the “img” tag on upload to preview the image. The second element is the “input” tag.
The React app we are going to build has a file input. When you select an image with this file input, an image preview will show up below it. There is also a “Remove This Image” button that lets you remove the selected image and the preview as well.
Right-click on an image file and you should now see an Image Preview command in the popup menu. Click that command to view the image in Windows Photo Viewer (Figure D). Photo Viewer instantly pops up. Click the magnifying glass icon and move the slider to zoom in or out of the image.
Answer: Use the JS readAsDataURL() Method You can use the JavaScript readAsDataURL() method of the FileReader object to read the contents of the specified file. When the read operation is finished, the readyState becomes DONE, and the loadend is triggered. The FileReader result property returns the file's contents.
Here is jQuery version for you. I think it more simplest thing.
$(function() { // Multiple images preview in browser var imagesPreview = function(input, placeToInsertImagePreview) { if (input.files) { var filesAmount = input.files.length; for (i = 0; i < filesAmount; i++) { var reader = new FileReader(); reader.onload = function(event) { $($.parseHTML('<img>')).attr('src', event.target.result).appendTo(placeToInsertImagePreview); } reader.readAsDataURL(input.files[i]); } } }; $('#gallery-photo-add').on('change', function() { imagesPreview(this, 'div.gallery'); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" multiple id="gallery-photo-add"> <div class="gallery"></div>
Add the multiple
attribute to your input
element.
function previewImages() { var preview = document.querySelector('#preview'); if (this.files) { [].forEach.call(this.files, readAndPreview); } function readAndPreview(file) { // Make sure `file.name` matches our extensions criteria if (!/\.(jpe?g|png|gif)$/i.test(file.name)) { return alert(file.name + " is not an image"); } // else... var reader = new FileReader(); reader.addEventListener("load", function() { var image = new Image(); image.height = 100; image.title = file.name; image.src = this.result; preview.appendChild(image); }); reader.readAsDataURL(file); } } document.querySelector('#file-input').addEventListener("change", previewImages);
<input id="file-input" type="file" multiple> <div id="preview"></div>
function previewImages() { var $preview = $('#preview').empty(); if (this.files) $.each(this.files, readAndPreview); function readAndPreview(i, file) { if (!/\.(jpe?g|png|gif)$/i.test(file.name)){ return alert(file.name +" is not an image"); } // else... var reader = new FileReader(); $(reader).on("load", function() { $preview.append($("<img/>", {src:this.result, height:100})); }); reader.readAsDataURL(file); } } $('#file-input').on("change", previewImages);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="file-input" type="file" multiple> <div id="preview"></div>
More info on MDN readAsDataURL
StackOverflow Preview Image, get file size, image height and width before upload
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With