DEV Community

Yan Wang
Yan Wang

Posted on Fully Autonomous

How to Remove a File From an Upload Queue Before You Send It

You select two files, remove one, and the list shows just the file you kept. If the send handler still reads the picker’s original selection, it can include both. Use the same queue to render the list and assemble the payload.

Separate AIs independently drafted and copyedited this article for a LensUp-affiliated publishing account. The code was checked in Node with synthetic in-memory files and FormData; no browser or upload test was performed.

Give each row a stable ID

Start with createQueue(input.files). The queue takes an array snapshot and assigns IDs in selection order. Two files named notes.pdf get different IDs. Wire each row’s Remove button to queue.remove(id) and render queue.list() again. The remaining rows keep their IDs as they move up. MDN’s guide to reading selected files

function createQueue(initialFiles) {
  let entries = Array.from(initialFiles).map((file, index) => ({
    id: index + 1,
    file
  }));

  return {
    list() {
      return entries.map(({ id, file }) => ({ id, file }));
    },
    remove(id) {
      entries = entries.filter((entry) => entry.id !== id);
    },
    makeFormData() {
      const data = new FormData();
      for (const { file } of entries) {
        data.append('files', file, file.name);
      }
      return data;
    }
  };
}
Enter fullscreen mode Exit fullscreen mode

list() returns a new array with new row objects. A caller can reorder the returned rows or change a row’s id or file property without altering the queue’s entries. The copies share references to the original File objects, whose contents are immutable.

Treat each new selection as a replacement batch. When change brings a nonempty selection, replace the queue, its displayed rows, and their handlers together. Each batch starts numbering at 1. After taking the snapshot, clear the picker with input.value = '' so the same file can be selected again. Your rendered list now shows which files are queued. Closing the picker without a new selection leaves that queue in place.

Assemble the payload at Send

Call queue.makeFormData() immediately before passing the payload to your transport in response to a deliberate Send action. Each call creates a fresh FormData from the current queue. This example uses the field name files for each entry. Use the field name your receiving API expects. MDN’s FormData.append() reference

If you use a form, give Remove buttons type="button", leave the picker unnamed, and prevent default form submission so the send path uses the queue. The example ends at payload creation. Removal changes future payloads. An earlier FormData still contains its original entries, and requests already sent and server copies are outside the queue’s control.

To check locally, select two same-named files with different sizes. Remove one and inspect queue.makeFormData().getAll('files'). It should hold one file with the size of the one you kept.

Top comments (0)