Create Image Thumbnails

This commit is contained in:
LSvekis
2021-07-02 10:38:50 -04:00
committed by GitHub
parent 90c20c08e1
commit 9f6262b175
+37
View File
@@ -0,0 +1,37 @@
<!doctype html>
<html>
<head>
    <title>Complete JavaScript Course</title>
<style>
.thumb {
max-height: 100px;
}
</style>
</head>
<body>
<input type="file" multiple accept="image/*" />
<div class="output"></div>
<script>
const message = document.getElementById("message");
const output = document.querySelector(".output");
const myInput = document.querySelector("input");
myInput.addEventListener("change", uploadAndReadFile);
function uploadAndReadFile(e) {
const files = e.target.files;
for (let i = 0; i < files.length; i++) {
const file = files[i];
const img = document.createElement("img");
img.classList.add("thumb");
img.file = file;
output.appendChild(img); // Assuming that "preview" is the div output where the content will be displayed.
const reader = new FileReader();
reader.onload = (function (aImg) { return function (e) { aImg.src = e.target.result; }; })(img);
reader.readAsDataURL(file);
}
}
</script>
</body>
</html>