File size: 1,385 Bytes
b828b8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Get references to HTML elements
const uploadButton = document.getElementById("upload-button");
const imageUpload = document.getElementById("image-upload");
const resultsOutput = document.getElementById("results-output");

// Function to handle image upload
function handleImageUpload() {
    const file = imageUpload.files[0];
    if (!file) {
        alert("Please select an image file.");
        return;
    }

    // Create a FormData object to send the image file to the server
    const formData = new FormData();
    formData.append("file", file);

    // Send the image to the server using fetch
    fetch("/", {
        method: "POST",
        body: formData,
    })
        // .then((response) => response.json())
        // .then((data) => {
        //     // Display the results in the resultsOutput element
        //     resultsOutput.innerHTML = "<h2>Results</h2>";
        //     for (const key in data) {
        //         if (data.hasOwnProperty(key)) {
        //             resultsOutput.innerHTML += `<p>${key}: ${data[key]}</p>`;
        //         }
        //     }
        // })
        .catch((error) => {
            console.error("Error uploading image:", error);
            alert("Error uploading image. Please try again.");
        });
}

// Add a click event listener to the upload button
uploadButton.addEventListener("click", handleImageUpload);