Probably a basic question, I need the formData.delete(key) function to delete the value of the "file[]" filename key. It's my understanding the the "file" value in formData.append is the uploaded file, and by deleting the value, I remove the file. I'm not sure what to subsitute for "key" in formData.delete(key) to reference the specific "file[]" key name.
As of now, I get 'Uncaught ReferenceError: key is not defined', expectedly, when I click the deleteButton element.
const formData = new FormData();
dropZone.addEventListener("drop", (event) => {
for (const file of event.dataTransfer.files) {
formData.append("file[]", file);
const listItem = document.createElement("li");
listItem.textContent = file.name + " ";
console.log(file.name);
const deleteButton = document.createElement("button");
deleteButton.classList.add("button", "button-small");
deleteButton.textContent = " Remove";
deleteButton.addEventListener("click", () => {
formData.delete(key);
listItem.remove();
});
listItem.appendChild(deleteButton);
filesList.appendChild(listItem);
}
});
I tried defining a local variable fileName = "file[]" and passing that in as the appended key, and passing that as the key name in formData.delete. This stopped any files from being appended in the ultimate post request, seemingly. This was kind of shooting in the dark, as my Javascript isn't very good.
const formData = new FormData();
dropZone.addEventListener("drop", (event) => {
for (const file of event.dataTransfer.files) {
var fileName = "file[]";
formData.append(fileName, file);
const listItem = document.createElement("li");
listItem.textContent = file.name + " ";
console.log(file.name);
const deleteButton = document.createElement("button");
deleteButton.classList.add("button", "button-small");
deleteButton.textContent = " Remove";
deleteButton.addEventListener("click", () => {
formData.delete(fileName);
listItem.remove();
});
listItem.appendChild(deleteButton);
filesList.appendChild(listItem);
}
});
I have tried formData.delete(file), but that is not removing the file from the ultimate post request.
keysupposed to be? If you want to be able to remove items fromformData, you have to give them unique names, not the samefile[]name for all of them.formDataas the user drops files, put them in an array, and you can remove from the array when they click the delete button. When they submit, createformDatafrom the array.