Site icon Filestack Blog

How to Add Folder Upload to Your Web Application

Modern web applications often need to handle dozens or even thousands of files at once. For products like document managers, cloud storage platforms, design tools, or CMSs, asking users to upload files one by one quickly becomes frustrating.

That’s where folder upload makes a real difference. Allowing users to upload entire folders (while preserving structure) saves time, reduces errors, and creates a far smoother experience, especially in production apps with real users.

This guide walks through:

Why Folder Upload Matters

Before getting into the implementation, it’s important to understand why folder upload is so useful in modern web apps:

Once you understand the value of folder upload, the next question is how to implement it on the web.

The Native JavaScript Approach

Modern browsers allow folder uploads using the HTML5 File API. This is done with a special attribute called webkitdirectory.

Setting Up the HTML

<input
  type="file"
  id="folderInput"
  webkitdirectory
  directory
  multiple
/>
<div id="fileList"></div>

Here,

When a user clicks this input, the file picker opens and asks them to choose a folder. All files inside that folder (including subfolders) are then available in JavaScript.

 

Handling the Upload in JavaScript

Now, let’s write JavaScript code to detect when a user selects a folder, access all the files inside it, display their details, and trigger the upload process.

const folderInput = document.getElementById('folderInput');
const fileList = document.getElementById('fileList');

folderInput.addEventListener('change', function(event) {
  const files = event.target.files;

  // Convert FileList to array for easier manipulation
  const fileArray = Array.from(files);

  // Display selected files with their paths
  fileList.innerHTML = '<h3>Selected Files:</h3>';
  fileArray.forEach(file => {
    const relativePath = file.webkitRelativePath || file.name;
    fileList.innerHTML += `<p>${relativePath} (${formatFileSize(file.size)})</p>`;
  });

  // Upload files
  uploadFolder(fileArray);
});

function formatFileSize(bytes) {
  if (bytes === 0) return '0 Bytes';
  const k = 1024;
  const sizes = ['Bytes', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}

What this code does:

 

Uploading Files to Your Server

Once the files are ready, you can send them to your server using FormData and the Fetch API.

async function uploadFolder(files) {
  const formData = new FormData();

  // Add each file to the FormData object
  files.forEach((file, index) => {
    // Preserve the folder structure in the form data
    formData.append('files[]', file, file.webkitRelativePath);
  });

  try {
    const response = await fetch('/api/upload', {
      method: 'POST',
      body: formData
    });

    if (response.ok) {
      const result = await response.json();
      console.log('Upload successful:', result);
    } else {
      console.error('Upload failed:', response.statusText);
    }
  } catch (error) {
    console.error('Upload error:', error);
  }
}

What this code does:

This approach works well for basic folder uploads and is supported by most modern browsers.

Limitations of the Native Approach

While this native solution works, it can be difficult to manage in real-world production apps:

At this point, the challenge is no longer selecting files, but handling reliability, scale, and user experience without building everything from scratch.

A Production-Ready Solution with Filestack

In production environments, building all these features from scratch can take a lot of time and effort. Filestack offers a ready-made folder upload solution that takes care of these challenges for you, while also providing a clean user interface and a reliable, scalable infrastructure.

Setting Up Filestack

First, create a Filestack account to get your API key. After that, add the Filestack CDN to your project so you can start using its upload features.

<script src="<https://static.filestackapi.com/filestack-js/4.x.x/filestack.min.js>"></script>

Implementing Folder Upload with Filestack

Here’s how simple folder upload becomes with Filestack:

// Initialise the Filestack client
const client = filestack.init("YOUR_API_KEY");

// Configure the picker with folder upload enabled
const options = {
  accept: ["*/*"], // Accept all file types
  maxFiles: 1000, // Maximum files per upload
  uploadInBackground: false,
  onUploadDone: (result) => {
    console.log("Upload complete:", result);
    result.filesUploaded.forEach((file) => {
      console.log(`Uploaded: ${file.filename} - ${file.url}`);
    });
  },
};

// Open the picker
const picker = client.picker(options);
document.getElementById("uploadBtn").addEventListener("click", () => {
  picker.open();
});

What this does:

With both approaches in mind, it’s easier to see how they compare in real development scenarios.

Comparing Approaches

Let’s look at the main differences between building a folder upload yourself and using Filestack:

Best Practices for Folder Upload

No matter which method you use, these best practices will help create a better upload experience:

Choosing the right folder upload strategy ultimately depends on your application’s scale, users, and long-term maintenance needs.

Conclusion

Folder upload changes how users work with applications that handle lots of files. While JavaScript offers a native way to upload folders using the File API and the webkitdirectory attribute, real-world applications often need something more reliable.

The native approach is fine for basic use cases or learning, but it becomes hard to manage as your app grows. Tools like Filestack remove this complexity by handling uploads, security, and performance for you. This lets your team focus on building meaningful features instead of dealing with upload logic.

If file handling is a core part of your product, Filestack offers a strong set of features, reliable infrastructure, and an easy-to-use API. You can start with its free tier and scale as your application grows.

The future of web applications demands seamless file handling. With the right folder upload implementation, you give your users the tools they need to work efficiently while maintaining the security and reliability your application requires.

Exit mobile version