Sling Academy
Home/JavaScript/Enhance File Uploads with Directory Selections in JavaScript

Enhance File Uploads with Directory Selections in JavaScript

Last updated: December 12, 2024

File uploading in web applications is a common necessity. Allowing users to select files to upload can enhance user experience, especially when they need to upload multiple files at once. In this article, we’ll explore how to enhance file uploads in JavaScript by allowing directory selections.

Introduction to File and Directory Uploads

Traditionally, when uploading files via a web page, users can select files using the file input element. The current modern browsers offer additional features by allowing not just files, but directories to be uploaded, which can simplify the process when dealing with multiple files.

The Basics of HTML File Inputs

The fundamental part of handling user file selections is through the <input> element with a type of file. Here is a basic example:

<input type="file" id="fileInput" multiple>

By adding multiple attribute, you enable users to pick multiple files. However, this alone doesn't allow directory selection.

Allowing Directory Selection

By adding the webkitdirectory attribute (specific for Webkit-based browsers, though supported now in most), the input element can allow users to select directories.

<input type="file" id="fileInput" webkitdirectory>

With this attribute, the file chooser dialog will let users pick a folder, and all its contents can then be retrieved in JavaScript.

Handling Directory Uploads in JavaScript

Once you've allowed directory selections via the input element, handling these selections requires JavaScript. Access the files property to manage all selected files.

document.getElementById('fileInput').addEventListener('change', function(event) { 
    const files = event.target.files; 
    for (let i = 0; i < files.length; i++) { 
        console.log(files[i].name); 
    }
}, false);

This example handles the change event to log the names of all files within the selected directory.

Running a Real-World Example

Let’s create a basic setup utilizing an HTML file input and a display area where you can list out all files selected by the user for better visualization.

<input type="file" id="directoryInput" webkitdirectory> 
<div id="fileList"></div>
document.getElementById('directoryInput').addEventListener('change', function(event) { 
    const files = event.target.files;
    const fileList = document.getElementById('fileList');
    fileList.innerHTML = ''; 
    for (let i = 0; i < files.length; i++) { 
        const listItem = document.createElement('div');
        listItem.textContent = `File Name: ${files[i].name} - File Size: ${files[i].size} Bytes`;
        fileList.appendChild(listItem);
    }
}, false);

After the user selects a directory, this code will display each file's name and size in a simple list under the input.

Conclusion

Enhancing your application's file upload capability to support directory selections can significantly improve user experience. With just a few lines of code, users can upload entire directories, reducing their effort and improving the efficiency of your workflow. As browser support continues to grow across all platforms for this feature, incorporating directory-based file uploads in your projects becomes increasingly viable.

Next Article: Organize Files and Folders with the JavaScript File and Directory Entries API

Previous Article: Bulk File Management Using the File and Directory Entries API in JavaScript

Series: Web APIs – JavaScript Tutorials

JavaScript

You May Also Like

  • Handle Zoom and Scroll with the Visual Viewport API in JavaScript
  • Improve Security Posture Using JavaScript Trusted Types
  • Allow Seamless Device Switching Using JavaScript Remote Playback
  • Update Content Proactively with the JavaScript Push API
  • Simplify Tooltip and Dropdown Creation via JavaScript Popover API
  • Improve User Experience Through Performance Metrics in JavaScript
  • Coordinate Workers Using Channel Messaging in JavaScript
  • Exchange Data Between Iframes Using Channel Messaging in JavaScript
  • Manipulating Time Zones in JavaScript Without Libraries
  • Solving Simple Algebraic Equations Using JavaScript Math Functions
  • Emulating Traditional OOP Constructs with JavaScript Classes
  • Smoothing Out User Flows: Focus Management Techniques in JavaScript
  • Creating Dynamic Timers and Counters with JavaScript
  • Implement Old-School Data Fetching Using JavaScript XMLHttpRequest
  • Load Dynamic Content Without Reloading via XMLHttpRequest in JavaScript
  • Manage Error Handling and Timeouts Using XMLHttpRequest in JavaScript
  • Handle XML and JSON Responses via JavaScript XMLHttpRequest
  • Make AJAX Requests with XMLHttpRequest in JavaScript
  • Customize Subtitle Styling Using JavaScript WebVTT Integration