Made it possible to add files via the selector "File by File".

This commit is contained in:
ge64qev 2024-05-06 11:18:59 +02:00
parent f7aebf22c8
commit 7ba0067688
2 changed files with 19 additions and 7 deletions

View file

@ -47,12 +47,11 @@ function setupFileInput(chooser) {
const dt = e.dataTransfer; const dt = e.dataTransfer;
const files = dt.files; const files = dt.files;
for (let i = 0; i < files.length; i++) { //Do not Update allFiles array here to prevent duplication, the change event listener will take care of that
allFiles.push(files[i]);
}
const dataTransfer = new DataTransfer(); const dataTransfer = new DataTransfer();
allFiles.forEach((file) => dataTransfer.items.add(file)); for (let i = 0; i < files.length; i++) {
dataTransfer.items.add(files[i]);
}
const fileInput = document.getElementById(elementId); const fileInput = document.getElementById(elementId);
fileInput.files = dataTransfer.files; fileInput.files = dataTransfer.files;
@ -81,8 +80,21 @@ function setupFileInput(chooser) {
document.body.addEventListener("drop", dropListener); document.body.addEventListener("drop", dropListener);
$("#" + elementId).on("change", function (e) { $("#" + elementId).on("change", function (e) {
allFiles = Array.from(e.target.files); //Get newly Added Files
const newFiles = Array.from(e.target.files);
//Add Files to existing Files
allFiles = allFiles.concat(newFiles);
//Update the file inout`s files property
const dataTransfer = new DataTransfer();
allFiles.forEach((file) => dataTransfer.items.add(file));
e.target.files = dataTransfer.files;
handleFileInputChange(this); handleFileInputChange(this);
//Call the displayFiles function with the allFiles array
displayFiles(allFiles)
}); });
function handleFileInputChange(inputElement) { function handleFileInputChange(inputElement) {

View file

@ -9,7 +9,7 @@ document.getElementById("fileInput-input").addEventListener("change", function (
}); });
/** /**
* @param {FileList} files * @param {File[]} files
*/ */
function displayFiles(files) { function displayFiles(files) {
const list = document.getElementById("selectedFiles"); const list = document.getElementById("selectedFiles");