Plant Detection Form
The code overall handles:
- the selection of multiple image files
- conversion of files to Base64 strings
- making the API request with the appropriate data
- displaying the API results in the UI (which consists of generating HTML through
JavaScript)
document.getElementById("PD-image-upload")
.addEventListener("change", function(event) {
var selectedFilesContainer = document.querySelector(".PD-selected-files");
selectedFilesContainer.innerHTML = "";
for (var i = 0; i < event.target.files.length; i++) {
var file = event.target.files[i];
var reader = new FileReader();
reader.onload = function(event) {
var image = document.createElement("img");
image.src = event.target.result;
var imageContainer = document.createElement("div");
imageContainer.appendChild(image);
var removeImage = document.createElement("span");
removeImage.classList.add("remove-image");
removeImage.innerHTML = "Remove";
removeImage.addEventListener("click", function() {
imageContainer.parentNode.removeChild(imageContainer);
});
imageContainer.appendChild(removeImage);
selectedFilesContainer.appendChild(imageContainer);
}
reader.readAsDataURL(file);
}
});