I have a project where the user selects images through an input element of type files and the selected images are appended in a container, but if, on a file upload that isn't the first, the files are the same as the ones before, the 'change' event listener doesn't trigger.
Here is the code for it:
function readURL(file) {
return new Promise(function(resolve, reject) {
let fr = new FileReader();
fr.onload = function() {
resolve(fr.result);
}
fr.onerror = function() {
reject(fr);
}
fr.readAsDataURL(file);
});
}
document.querySelector("#file").addEventListener("change", function(event) {
let files = event.currentTarget.files;
let readers = []
if (!files.length) return;
for (let i=0; i<files.length; i++) {
readers.push(readURL(files[i]));
}
readers.reverse();
Promise.all(readers)
.then(function(values) {
values.forEach(item => {
let image = document.createElement("img");
image.className = "imgs";
image.src = item;
document.querySelector("#messages").appendChild(image);
});
})
.catch(function(err) {
console.log(err);
});
}, false);
body {
width: 100vw;
height: 100vh;
margin: 0;
display: grid;
grid-template-columns: 1fr 4fr;
grid-template-rows: 100vh;
overflow: hidden;
}
.contacts {
grid-column: 1 / span 1;
background-color: darkslategray;
}
body > div {
grid-column: 2 / span 2;
grid-template-rows: 6fr 1fr;
display: grid;
grid-template-rows: 6fr 1fr;
}
#messages {
padding: 0 20px 10px 0;
display: flex;
flex-direction: column-reverse;
align-items: flex-end;
overflow-x: hidden;
overflow-y: scroll;
}
#text {
background-color: brown;
padding: 3px 5px;
}
.interact {
background-color: darkgray;
display: flex;
justify-content:center;
align-items: center;
}
#btn {
background-color: cadetblue;
width: 50px;
height: 50px;
display: flex;
justify-content: center;
align-items: center;
font-size: 30px;
font-weight: bold;
border-radius: 50%;
cursor: pointer;
}
.imgs {
max-height: 300px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="script.js" defer></script>
<title>Document</title>
</head>
<body>
<div class="contacts"></div>
<div>
<div id="messages">
</div>
<div class="interact">
<div id="btn" onclick="document.getElementById('file').click()"> + </div>
</div>
</div>
<input type="file" accept="image/*" id="file" multiple>
</body>
</html>
Here is a jsfiddle version as well.