0

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.

3
  • What is key supposed to be? If you want to be able to remove items from formData, you have to give them unique names, not the same file[] name for all of them. Commented Aug 30, 2023 at 20:40
  • Instead of updating formData as 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, create formData from the array. Commented Aug 30, 2023 at 20:43
  • @Barmar, I realize that I was passing the same "file[]" string for everything. I replaced the .append key argument with file.name to give them individual names, and this worked. I believe we can delete objects with .delete now on most browsers. Thank you! Commented Aug 30, 2023 at 21:17

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.