Course contentsShow
← Web Development
Lesson 2683 of 2,98248. File Uploads and HandlingFree lesson

Multiple File Drops

Handling DataTransfer.files as a FileList when multiple files are dropped simultaneously.

Multiple File Drops

What you'll learn: How to handle multiple files being dropped at once using the DataTransfer API's files property.

When users drag and drop files onto your drop zone, they're not limited to one at a time—they can select and drop an entire batch. The DataTransfer object you receive in the drop event gives you access to all dropped files through its files property, which returns a FileList object.

Understanding FileList from Drops

A FileList is an array-like collection, similar to what you get from a file input element. Think of it as a container holding multiple File objects. You can check its length property to see how many files were dropped and access individual files using bracket notation.

dropZone.addEventListener('drop', (e) => {
  e.preventDefault();
  
  const files = e.dataTransfer.files;
  
  console.log(`${files.length} file(s) dropped`);
  
  // Loop through all dropped files
  for (let i = 0; i < files.length; i++) {
    const file = files[i];
    console.log(`File ${i + 1}: ${file.name}, ${file.size} bytes`);
  }
});

Processing Multiple Files

You can iterate through the FileList using a traditional for loop (shown above) or convert it to an array for modern iteration methods:

const fileArray = Array.from(e.dataTransfer.files);

fileArray.forEach(file => {
  console.log(file.name);
  // Process each file individually
});

Each File object in the list has the same properties you'd expect: name, size, type, and lastModified. You can validate and process each file independently or as a batch.

Key Takeaway: The DataTransfer.files property returns a FileList containing all simultaneously dropped files, which you iterate through to process each file individually.