To upload multiple files and create thumbnails I'm using code:
<form class="article-comments__form">
<textarea class="article-comments__textarea" placeholder="Join the discussion…"></textarea>
<div class='article-comments__comment-section--footer'>
<div class="article-comments__uploadedFiles"></div>
<button class="article-comments__upload-image">
<input class="imgUploadInput" type="file" name="imgfile" accept="image/*" multiple />
</button>
<span class="article-comments__upload--description">Add Images</span>
<button class="article-comments__postButton disabled" type="submit" aria-label="Post">Post</button>
</div>
</form>
imgUploadInput.onchange = (e) => {
const files = e.target.files;
const output = imgUploadInput.parentNode.querySelector('.article-comments__uploadedFiles');
// create and add thumbnails to Uploaded Files Block
for (let i = 0; i < files.length; i++) {
if (!files[i].type.match("image")) continue;
const picReader = new FileReader();
picReader.addEventListener("load", function (event) {
const picFile = event.target;
const div = document.createElement("div");
div.classList.add('image')
div.innerHTML = `<img class="article-comments__thumbnail" src="${picFile.result}"/>`;
output.appendChild(div);
});
picReader.readAsDataURL(files[i]);
}
}
The code above is working perfectly if I select multiple files at once. What I need is to upload multiple images adding an image one at a time, is there a way to do that with html & JavaScript? Each time I try to upload another image the old one is deleted from input(file type) and I get to submit just la last added image.
Could someone please provide some resources or a code snippet? The code above is displaying the thumbnail but the input is not keeping the previous loaded images.