-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (46 loc) · 1.47 KB
/
script.js
File metadata and controls
55 lines (46 loc) · 1.47 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
document
.getElementById("submitBtn")
.addEventListener("click", async function () {
const fileInput = document.getElementById("fileInput");
const fileType = document.querySelector(
'input[name="fileType"]:checked',
).value;
const file = fileInput.files[0];
if (!file) {
alert("Please select a file");
return;
}
if (!fileType) {
alert("Please select a file type");
return;
}
const endpoint = "/" + fileType + "s";
const formData = new FormData();
formData.append("upload_file", file);
try {
this.classList.add("loading");
this.disabled = true;
const response = await fetch(endpoint, {
method: "POST",
body: formData,
});
// Check if request was successful
if (!response.ok) {
const detail = await response.text();
document.getElementById("errorText").textContent = detail;
document.getElementById("errorSection").classList.remove("hidden");
}
// Parse JSON response
const result = await response.json();
// Display result
document.getElementById("resultText").textContent = result.final_result;
document.getElementById("resultSection").classList.remove("hidden");
} catch (error) {
console.error("Upload error:", error);
alert("Upload failed: " + error.message);
} finally {
// Remove loading state
this.classList.remove("loading");
this.disabled = false;
}
});