working file upload

This commit is contained in:
C4illin 2024-05-17 19:23:05 +02:00
parent 787ff9741e
commit a0885013bb
6 changed files with 66 additions and 15 deletions

View file

@ -1,9 +1,11 @@
// Select the file input element
const fileInput = document.querySelector('input[type="file"]');
let filesToUpload = [];
// Add a 'change' event listener to the file input element
fileInput.addEventListener("change", (e) => {
console.log(e.target.files);
console.log(e.target.files);
// Get the selected files from the event target
const files = e.target.files;
@ -23,4 +25,24 @@ fileInput.addEventListener("change", (e) => {
// Append the row to the file-list table
fileList.appendChild(row);
}
uploadFiles(files);
});
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));
};