Detect folders in Safari with HTML5

Safari has limited support for folder detection compared to other browsers. When users drag and drop folders, Safari treats them differently than individual files, which can cause errors during file reading operations.

The Problem with Folders in Safari

When a folder is dropped onto a web page, Safari includes it in the files collection but cannot read its contents using FileReader. This causes the onerror event to trigger, which we can use to detect folder drops.

Folder Detection Method

The following code attempts to read each dropped item as a file. If it's a folder, the FileReader will fail and trigger the error handler:

function handleDrop(e) {
    e.preventDefault();
    
    Array.prototype.forEach.call(e.dataTransfer.files, function (file) {
        var r = new FileReader();
        
        r.onload = function (event) {
            // File read successfully - it's a valid file
            console.log("File detected:", file.name);
            addFile(file);
        };
        
        r.onerror = function (event) {
            // Error reading file - likely a folder
            alert("Uploading folders isn't supported in Safari browser!");
            console.log("Folder detected:", file.name);
        };
        
        // Attempt to read the file
        r.readAsDataURL(file);
    });
}

// Set up drop zone
var dropZone = document.getElementById('dropZone');
dropZone.addEventListener('drop', handleDrop);
dropZone.addEventListener('dragover', function(e) {
    e.preventDefault();
});

Complete Example

<!DOCTYPE html>
<html>
<head>
    <title>Safari Folder Detection</title>
    <style>
        #dropZone {
            width: 300px;
            height: 200px;
            border: 2px dashed #ccc;
            text-align: center;
            padding: 50px;
            margin: 20px;
        }
    </style>
</head>
<body>
    <div id="dropZone">
        Drop files or folders here
    </div>
    
    <script>
        function addFile(file) {
            console.log("Processing file:", file.name, "Size:", file.size);
        }
        
        function handleDrop(e) {
            e.preventDefault();
            
            Array.prototype.forEach.call(e.dataTransfer.files, function (file) {
                var r = new FileReader();
                
                r.onload = function (event) {
                    addFile(file);
                };
                
                r.onerror = function (event) {
                    alert("Uploading folders isn't supported in Safari browser!");
                };
                
                r.readAsDataURL(file);
            });
        }
        
        var dropZone = document.getElementById('dropZone');
        dropZone.addEventListener('drop', handleDrop);
        dropZone.addEventListener('dragover', function(e) {
            e.preventDefault();
        });
    </script>
</body>
</html>

How It Works

The detection relies on Safari's inability to read folder contents:

  1. File Drop: User drops items onto the drop zone
  2. FileReader Attempt: Code tries to read each item using readAsDataURL()
  3. Success: If it's a file, onload fires and the file is processed
  4. Error: If it's a folder, onerror fires, indicating folder detection

Browser Limitations

Browser Folder Support Detection Method
Chrome/Edge Full support webkitdirectory attribute
Firefox Partial support File API properties
Safari Limited FileReader error detection

Conclusion

Safari's folder detection relies on catching FileReader errors when attempting to read folder contents. While this method works for detection, Safari cannot actually process folder contents like other browsers.

Updated on: 2026-03-15T23:18:59+05:30

190 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements