DirectoryReader

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The DirectoryReader interface of the File System API lets you read the entries in a directory.

About this document

This document was last updated on March 1, 2012 and follows the W3C Specifications (Working Draft) drafted on April 19, 2011.

This specification is abandoned, having failed to reach any substantial traction.

Basic concepts

The only method for this interface, readEntries() is for listing all the files and folders in a directory. To list all the entries, you need to do the following:

  1. Call directoryEntry.createReader() to create a new DirectoryReader.
  2. Call readEntries().
  3. Continue calling readEntries() until an empty array is returned. You have to do this because the API might not return all entries in a single call.

Example

In the following code snippet from HTML5Rocks, we list all the entries in a directory.

// Taking care of the browser-specific prefixes.
window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem; 
window.directoryEntry = window.directoryEntry || window.webkitDirectoryEntry;

...

function toArray(list) {
  return Array.prototype.slice.call(list || [], 0);
}

function listResults(entries) {
  // To improve performance, we create document fragments, 
  // which are appended to the DOM only once. 
  // So only one browser reflow occurs.
  var fragment = document.createDocumentFragment();

  entries.forEach(function(entry, i) {
    var img = entry.isDirectory ? 'img src=<"folder-icon.gif">' :
                                  'img src=<"file-icon.gif">';
                                  
                     
    var li = document.createElement('li');
    li.innerHTML = [img, '', entry.name, ''].join('');
    fragment.appendChild(li);
  });

  document.querySelector('#filelist').appendChild(fragment);
}

function onInitFs(fs) {

  var dirReader = fs.root.createReader();
  var entries = [];

  // Keep calling readEntries() until no more results are returned.
  var readEntries = function() {
     dirReader.readEntries (function(results) {
      if (!results.length) {
        listResults(entries.sort());
      } else {
        entries = entries.concat(toArray(results));
        readEntries();
      }
    }, errorHandler);
  };

// Start reading the directory.

  readEntries(); 

}

// Creating a filesystem
window.requestFileSystem(window.TEMPORARY, 1024*1024, onInitFs, errorHandler);

Method overview

void readEntries (EntriesCallback successCallback, optional ErrorCallback errorCallback);

Method

readEntries()

Returns a list of entries from a specific directory. Call this method until an empty array is returned.

void readEntries (
  in EntriesCallback successCallback, optional ErrorCallback errorCallback
);
successCallback
Called once for every successful call to readEntries(). It delivers the next previously unreported set of entries in the associated directory until an empty array is returned.
errorCallback
A callback indicating that there was an error reading from the directory.
Returns
void

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 13webkit Not supported Not supported Not supported Not supported
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support Not supported (Yes) webkit Not supported Not supported Not supported Not supported

See also

Specification: File API: Directories and System SpecificationWD

Reference: File System API

Introduction: Basic Concepts About the File System API

Document Tags and Contributors

 Contributors to this page: jswisher, idupree, Jeremie, teoli, kscarfone, Sheppy, grendel
 Last updated by: jswisher,