0

I am trying to preview multiple images before I upload it onto my site. Although mine isn't working for some reason. I am not getting any errors in the console. Below is the JS for it,

$(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');
});
});

This is the html

    <div class="input-group">

 <label for="file-upload" class="custom-file-upload" ">
 <i class="fa fa-cloud-upload"></i> Chosose File...
 </label>
 <input id="file-upload" type="file" multiple id="gallery-photo-add/>          
  <button class="btn btn-lg btn-primary btn-block btn-signin-upload" type="submit">Upload</button>

</div>  
<div class="gallery"></div>

Any help would be grateful, thanks

OL12_9
  • 63
  • 9
  • 1
    Possible duplicate of [Preview an image before it is uploaded](https://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded) – Heretic Monkey Feb 08 '18 at 18:03
  • @MikeMcCaughan I looked at this but my problem is, when I select the images I want to upload and click open they don't display anywhere in the front end – OL12_9 Feb 08 '18 at 18:09
  • The code in the accepted answer works... – Heretic Monkey Feb 08 '18 at 18:11
  • @MikeMcCaughan Yes I know it does, and that is why I asked this question because I am unsure why it doesn't work for me – OL12_9 Feb 08 '18 at 18:17

1 Answers1

0

Here you can find your correct code: https://jsfiddle.net/zhvdjxxo/1/ Seems like you've mistaken the ids in "onchange" function.

$(function() {

var imagesPreview = function(input, placeToInsertImagePreview) {

if (input.files) {
    console.log(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]);
    }
}

};

$('#file-upload').on('change', function() {
imagesPreview(this, 'div.gallery');
});  
});
Arty9000
  • 103
  • 8
  • I copied this but I am getting these errors in the console FileList {0: File(7211), 1: File(14288), length: 2}0: File(7211) {name: "china.gif", lastModified: 1430774921000, lastModifiedDate: Mon May 04 2015 22:28:41 GMT+0100 (GMT Summer Time), webkitRelativePath: "", size: 7211, …}1: File(14288) {name: "CHINA.jpg", lastModified: 1430775310000, lastModifiedDate: Mon May 04 2015 22:35:10 GMT+0100 (GMT Summer Time), webkitRelativePath: "",size: 14288, …}length: 2__proto__: FileList 2jquery.js:752 Uncaught TypeError: $.parseHTML is not a function at FileReader.reader.onload (jquery.js:752) – OL12_9 Feb 08 '18 at 18:13
  • Did you copy it from here or from JSfiddle? Try copy both html and js from jsfiddle, because there is 100% working example. Your html above (in the question) has some incorrects, ie input file tag can't have two ids. – Arty9000 Feb 08 '18 at 18:19
  • I copied it from both, but still showing the same error. If you want I can show you the website I am working on, but because it is for uni I will have to delete the link asap after I post it. – OL12_9 Feb 08 '18 at 18:22
  • lets see what i can help with :) – Arty9000 Feb 08 '18 at 18:27
  • Will be afk for about an hour, but will be back on later, if you know a solution, could you post it on this thread please, if not never worry. Thank you :) – OL12_9 Feb 08 '18 at 18:37
  • try this one -- https://jsfiddle.net/zhvdjxxo/2/ sorry don't have enough time to look into issue why $.parseHtml is not working on your side, but in my updated example i just change this function into pure JS DOM element creating. – Arty9000 Feb 08 '18 at 18:41
  • Glad i could help :) – Arty9000 Feb 09 '18 at 09:22