2

i have a little problem with image upload preview. Here's the preview of the element :

image element

And this is the code :

<div class="img-container">
  <img id="preview-1" src="" alt="" />
  <div class="more">
    <img id="preview-2" src="" alt="" />
    <img id="preview-3" src="" alt="" />
    <span>Max 3 images.</span>
  </div>
  <label for="upload-btn" class="button">
    <img src="ssts/img/svg/upload.svg" alt="" />
    <span>UPLOAD</span>
    <input id="upload-btn" name="upload" type="file" accept="image/*"/>
  </label>
</div>

My problem is, how do i upload more than one image in single button and it will automatically show the preview in the element ? And after all of the element filled, the upload button will change to "Remove" that if clicked it will remove all the preview ? Thanks in advance!

UPDATE

the most important question is, "showing all of choosen images, and automatically show to the preview"

Gilang Rizkie
  • 101
  • 10
  • http://stackoverflow.com/questions/1593225/how-to-select-multiple-files-with-input-type-file – DaniP Apr 20 '17 at 16:44
  • Do the preview and the remove ... I guess is a little complex process , have you searched/try anything ? – DaniP Apr 20 '17 at 16:45
  • You might be interested in [FileReader](https://developer.mozilla.org/fr/docs/Web/API/FileReader)... – Badacadabra Apr 20 '17 at 16:45
  • http://stackoverflow.com/questions/4459379/preview-an-image-before-it-is-uploaded – DaniP Apr 20 '17 at 16:46
  • 1
    google is your friend you just need to filter each step you want ... don't expect to find all the code for that you want – DaniP Apr 20 '17 at 16:46
  • i know multiple will select more than one image, but what i need is when i select all the images i want to uploaded it will automatically show to the preview @DaniP – Gilang Rizkie Apr 20 '17 at 16:49
  • just search check the second question I linked on the comments – DaniP Apr 20 '17 at 16:51
  • Possible duplicate of [multiple image upload and preview](http://stackoverflow.com/questions/20779983/multiple-image-upload-and-preview) check first answer of this question – Deep 3015 Apr 20 '17 at 16:51
  • Or duplicate of this http://stackoverflow.com/questions/13683579/how-to-provide-a-preview-for-multiple-images-selected-in-fileuplaod-using-jquery – DaniP Apr 20 '17 at 16:53
  • @DaniP here's the example, i have 3 images. First image will showing in the biggest one preview, second and third image will show in `more` element. In that question just showing all of selected image without `class`. – Gilang Rizkie Apr 20 '17 at 16:55
  • then just adjust that code to your needs ... try something – DaniP Apr 20 '17 at 16:56
  • @DaniP that's what i don't understand about.. – Gilang Rizkie Apr 20 '17 at 16:57
  • @Badacadabra i know this, another question has been answered and all of this comment things that i need `multiple` and mark this as duplicate, but this is totally different with the others. – Gilang Rizkie Apr 20 '17 at 16:58
  • Then you need to be more specific on your question ... as I said again those linked questions had the answer play with the styles chekc this updated http://jsfiddle.net/Yvgc2/1545/ – DaniP Apr 20 '17 at 17:10
  • @DaniP Thanks for keep answering my question, but that answer you linked to is just uploading image with the same `class` named `thumbnail` each image. What i need is different with that. The first one will show in the biggest one and the others will show in `more` element. – Gilang Rizkie Apr 20 '17 at 17:16

1 Answers1

3

You need the multiple attribute and you need to specify that the name is an array.

multiple (HTML5)

This Boolean attribute indicates whether the user can enter more than one value. This attribute applies when the type attribute is set to email or file, otherwise it is ignored.

<input id="upload-btn" name="upload[]" type="file" accept="image/*" multiple />

You can find a example using PHP here:

How can I select and upload multiple files with HTML and PHP, using HTTP POST?

The following demo was adapted from this:

Multiple image upload and preview

window.onload = function() {
  // Check for File API support.
  if (window.File && window.FileList && window.FileReader) {
    var filesInput = document.getElementById('upload-btn');
    filesInput.addEventListener('change', function(e) {
      var output = document.getElementById('result');
      var files = e.target.files; //FileList object
      
      output.innerHTML = ''; // Clear (previous) results.
      
      for (var i = 0; i < files.length; i++) {
        var currFile = files[i];
        if (!currFile.type.match('image')) continue; // Skip non-images.
        
        var imgReader = new FileReader();
        imgReader.fileName = currFile.name;
        imgReader.addEventListener('load', function(e1) {
          var img = e1.target;
          var div = document.createElement('div');
          div.className = 'thumbnail';
          div.innerHTML = [
            '<img src="' + img.result + '"' + 'title="' + img.fileName + '"/>',
            '<label class="caption">' + img.fileName + '</label>'
          ].join('');
          output.appendChild(div);
        });

        // Read image.
        imgReader.readAsDataURL(currFile);
      }
    });
  } else {
    console.log('Your browser does not support File API!');
  }
}
body {
  padding: 0;
  margin: 0;
}
header {
  background-color: #EEE;
  padding: 0.125em;
}
article {
  margin: 0.5em;
}
output {
  display: block;
}
.thumbnail {
  display: inline-block;
}
.thumbnail img {
  height: 100px;
  margin: 4px;
  border: 1px solid #444;
}
label.caption {
  display: block;
  text-align: center;
  font-style: italic;
  color: #444;
}
<header>
  <h1>File API - FileReader</h1>
</header>
<article>
  <label for="files">Select multiple files: </label>
  <input type="file" id="upload-btn" name="upload[]" accept="image/*" multiple />
  <output id="result" />
</article>

const IMAGE_LIMIT = 3;
window.onload = function() {
  // Check for File API support.
  if (window.File && window.FileList && window.FileReader) {
    var filesInput = document.getElementById('upload-btn');
    filesInput.addEventListener('change', function(e) {
      var output = document.getElementById('result');
      var files = e.target.files; //FileList object
      
      for (var i = 0; i < files.length; i++) {
        var currFile = files[i];
        if (!currFile.type.match('image')) continue; // Skip non-images.
        
        var imgReader = new FileReader();
        imgReader.fileName = currFile.name;
        imgReader.index = i;
        imgReader.addEventListener('load', function(e1) {
          var img = e1.target;
          var index = img.index;
          if (index < IMAGE_LIMIT) {
            var imgContainer = document.getElementById('preview-' + (index + 1));
            imgContainer.src = img.result;
            imgContainer.title = img.fileName
          }
        });

        // Read image.
        imgReader.readAsDataURL(currFile);
      }
    });
  } else {
    console.log('Your browser does not support File API!');
  }
}
body { padding: 0; margin: 0; background: #DADFE2; }
.img-container {
  width: 284px;
  padding: 0.5em;
  border: 1px solid #AAA;
  background: #EEEEEE;
}
img[id^="preview-"] {
  display: inline-block;
  border: border: 1px solid #777;
  margin: 0.25em;
  background: #DADFE2;
}
#preview-1 {
  width: 256px;
  height: 256px;
}
#preview-2, #preview-3 {
  width: 64px;
  height: 64px;
}
.button {
  display: inline-block;
  margin: 0.25em;
  padding: 0.5em;
  background: #FF6B6F;
  color: #FFF;
  width: 98%;
  text-align: center;
}
.button:hover { cursor: pointer; background: #ff8486; }
.button:active { background: #e55052; }
input[type="file"]#upload-btn {
  display: none;
}
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<div class="img-container">
  <img id="preview-1" src="" alt="" />
  <div class="more">
    <img id="preview-2" src="" alt="" />
    <img id="preview-3" src="" alt="" />
    <span>Max 3 images.</span>
  </div>
  <label for="upload-btn" class="button">
    <i class="fa fa-upload"></i>
    <span>Upload</span>
    <input type="file" id="upload-btn" name="upload[]"  accept="image/*" multiple />
  </label>
</div>
Community
  • 1
  • 1
Mr. Polywhirl
  • 31,606
  • 11
  • 65
  • 114
  • And how to preview all of those images automatically ? – Gilang Rizkie Apr 20 '17 at 16:49
  • here's the example, i have 3 images. First image will showing in the biggest one preview, second and third image will show in `more` element. In that question just showing all of selected image without `class` – Gilang Rizkie Apr 20 '17 at 16:59
  • @GilangRizkie I have added an example. – Mr. Polywhirl Apr 20 '17 at 17:05
  • How do i make the 1st image it showed in `preview-1` ? 2nd image in `preview-2` ? 3rd image in `preview-3` ? Your example only show in same `id`, i have its own `class` for the images. Thanks. – Gilang Rizkie Apr 20 '17 at 17:10
  • @GilangRizkie In the `imgReader.addEventListener('load')` function, just grab the current index (`i`) and set the appropriate image values instead of writing out to the results. – Mr. Polywhirl Apr 20 '17 at 17:16
  • so it will look like `imgReader.addEventListener('load[1]')` for showing the 1st one image ? and the rest will use default `imgReader.addEventListener('load')` without index inserted ? – Gilang Rizkie Apr 20 '17 at 17:19
  • @GilangRizkie OK, added a second example, it is hidden by default. Click it to expand. – Mr. Polywhirl Apr 20 '17 at 17:32
  • THANKS SIR, U SAVE MY DAY! ITS SOLVED!! – Gilang Rizkie Apr 20 '17 at 17:42
  • And last sir, if its add more that 3 images which images will stored to database if submitted ? – Gilang Rizkie Apr 20 '17 at 17:45
  • @GilangRizkie You have all the images here... `e.target.files` Just store their base64 data. But it is more proper to save the files to the server and save their file location to a database. Images take up too much space. – Mr. Polywhirl Apr 20 '17 at 17:51