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

Multiple File Upload Queue

Build a queue system for uploading multiple files sequentially or in parallel with individual progress indicators.

Multiple File Upload Queue

What you'll learn: How to build a queue system that uploads multiple files either sequentially (one after another) or in parallel (simultaneously) with individual progress tracking for each file.

Why You Need a Queue System

When users select multiple files, uploading them all at once can overwhelm the server and make it impossible to track individual file progress. A queue system lets you control the flow, providing clear feedback for each file and better resource management.

Think of it like a checkout line at a store: sequential uploads are one customer at a time (safe, predictable), while parallel uploads are multiple registers open at once (faster, but requires coordination).

Building the Queue

Your queue manages an array of files and processes them systematically:

class UploadQueue {
  constructor(files, options = {}) {
    this.files = Array.from(files);
    this.concurrent = options.concurrent || 1; // How many at once
    this.active = 0;
    this.completed = 0;
  }

  async start() {
    while (this.files.length > 0 || this.active > 0) {
      // Start new uploads if slots available
      while (this.active < this.concurrent && this.files.length > 0) {
        const file = this.files.shift();
        this.uploadFile(file);
      }
      await new Promise(resolve => setTimeout(resolve, 100));
    }
  }

  uploadFile(file) {
    this.active++;
    const xhr = new XMLHttpRequest();
    
    xhr.upload.onprogress = (e) => {
      const percent = (e.loaded / e.total) * 100;
      this.updateProgress(file.name, percent);
    };
    
    xhr.onload = () => {
      this.active--;
      this.completed++;
      this.updateProgress(file.name, 100);
    };

    // Upload logic here...
  }

  updateProgress(filename, percent) {
    // Update UI for this specific file
  }
}

Set concurrent: 1 for sequential, or concurrent: 3 for parallel uploads.

Key Takeaway: A file upload queue controls when and how many files upload simultaneously, providing individual progress feedback and preventing server overload through configurable concurrency limits.