Files
ConvertX/src/public/script.js
T

69 lines
1.7 KiB
JavaScript
Raw Normal View History

2024-04-10 14:42:05 +02:00
// Select the file input element
const fileInput = document.querySelector('input[type="file"]');
2024-05-18 00:16:08 +02:00
const filesToUpload = [];
2024-05-17 19:23:05 +02:00
2024-04-10 14:42:05 +02:00
// Add a 'change' event listener to the file input element
fileInput.addEventListener("change", (e) => {
2024-05-17 19:23:05 +02:00
console.log(e.target.files);
2024-04-10 14:42:05 +02:00
// Get the selected files from the event target
const files = e.target.files;
// Select the file-list table
const fileList = document.querySelector("#file-list");
// Loop through the selected files
for (let i = 0; i < files.length; i++) {
// Create a new table row for each file
const row = document.createElement("tr");
row.innerHTML = `
<td>${files[i].name}</td>
<td>${(files[i].size / 1024 / 1024).toFixed(2)} MB</td>
2024-05-18 00:16:08 +02:00
<td><button class="secondary" onclick="deleteRow(this)">x</button></td>
2024-04-10 14:42:05 +02:00
`;
// Append the row to the file-list table
fileList.appendChild(row);
}
2024-05-17 19:23:05 +02:00
uploadFiles(files);
2024-04-10 14:42:05 +02:00
});
2024-05-17 19:23:05 +02:00
2024-05-18 00:16:08 +02:00
// Add a onclick for the delete button
const deleteRow = (target) => {
2024-05-18 01:41:35 +02:00
const filename = target.parentElement.parentElement.children[0].textContent;
2024-05-18 00:16:08 +02:00
const row = target.parentElement.parentElement;
row.remove();
2024-05-18 01:41:35 +02:00
2024-05-18 00:16:08 +02:00
fetch("/delete", {
method: "POST",
2024-05-18 01:41:35 +02:00
body: JSON.stringify({ filename: filename }),
headers: {
"Content-Type": "application/json",
},
2024-05-18 00:16:08 +02:00
})
.then((res) => res.json())
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
};
2024-05-17 19:23:05 +02:00
const uploadFiles = (files) => {
const formData = new FormData();
for (let i = 0; i < files.length; i++) {
formData.append("files", files[i], files[i].name);
}
fetch("/upload", {
method: "POST",
body: formData,
})
.then((res) => res.json())
.then((data) => {
console.log(data);
})
.catch((err) => console.log(err));
};