Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 871603 of FileSystemDirectoryReader

  • Revision slug: Web/API/DirectoryReader
  • Revision title: DirectoryReader
  • Revision id: 871603
  • Created:
  • Creator: Jeremie
  • Is current revision? No
  • Comment

Revision Content

{{APIRef("File System API")}}{{Non-standard_header}}

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

This interface has been abandonned: it was on a standard track and it proves not a good idea. Do not use it anymore.

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 pretty much 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 direcoryEntry.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

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 13{{ property_prefix("webkit") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support {{ CompatNo() }} 0.16{{ property_prefix("webkit") }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

See also

Specification: {{ spec("https://dev.w3.org/2009/dap/file-system/pub/FileSystem/", "File API: Directories and System Specification", "WD") }}

Reference: File System API

Introduction: Basic Concepts About the File System API

Revision Source

<p>{{APIRef("File System API")}}{{Non-standard_header}}</p>

<p>The <code>DirectoryReader</code> interface of the <a href="/en/DOM/File_API/File_System_API" title="en/DOM/File_API/File_System_API">File System API</a> lets you read the entries in a directory.</p>

<div class="note">
<p>This interface has been abandonned: it was on a standard track and it proves not a good idea. Do not use it anymore.</p>
</div>

<h2 id="About_this_document">About this document</h2>

<p>This document was last updated on March 1, 2012 and follows the <a class="external" href="https://www.w3.org/TR/file-system-api/">W3C Specifications (Working Draft)</a> drafted on April 19, 2011.</p>

<p>This specification is pretty much abandoned, having failed to reach any substantial traction.</p>

<h2 id="basic_concepts" name="basic_concepts">Basic concepts</h2>

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

<ol>
 <li>Call <a href="/en/DOM/File_API/File_System_API/DirectoryEntry#createReader" title="en/DOM/File_API/File_System_API/DirectoryEntry#createReader"><code>direcoryEntry.createReader()</code></a> to create a new DirectoryReader.</li>
 <li>Call <code> <a href="#readEntries">readEntries()</a></code>.</li>
 <li>Continue calling <code>readEntries()</code> until an empty array is returned. You have to do this because the API might not return all entries in a single call.</li>
</ol>

<h4 id="example" name="example">Example</h4>

<p>In the following code snippet from <a class="external" href="https://www.html5rocks.com/en/tutorials/file/filesystem/">HTML5Rocks</a>, we list all the entries in a directory.</p>

<pre class="brush: js">
// Taking care of the browser-specific prefixes.
window.requestFileSystem&nbsp;&nbsp;=&nbsp;window.requestFileSystem&nbsp;||&nbsp;window.webkitRequestFileSystem;&nbsp;
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=&lt;"folder-icon.gif"&gt;' :
                                  'img src=&lt;"file-icon.gif"&gt;';
                                  
                     
    var li = document.createElement('li');
    li.innerHTML = [img, '<span>', entry.name, '</span>'].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);</pre>

<h2 id="Method_overview">Method overview</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <td><code>void <a href="#createReader" title="#readEntries">readEntries</a> (EntriesCallback successCallback, optional ErrorCallback errorCallback);</code></td>
  </tr>
 </tbody>
</table>

<h2 id="Method">Method</h2>

<h3 id="readEntries" name="readEntries">readEntries()</h3>

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

<pre>
void readEntries (
  in EntriesCallback successCallback, optional ErrorCallback errorCallback
);</pre>

<dl>
 <dt>successCallback</dt>
 <dd>Called once for every successful call to <code>readEntries()</code>. It delivers the next previously unreported set of entries in the associated directory until an empty array is returned.</dd>
 <dt>errorCallback</dt>
 <dd>A callback indicating that there was an error reading from the directory.</dd>
</dl>

<h5 id="Returns">Returns</h5>

<dl>
 <dt><code>void</code></dt>
</dl>

<h2 id="Browser_Compatibility" name="Browser_Compatibility">Browser compatibility</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>13{{ property_prefix("webkit") }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome for Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE&nbsp;Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{ CompatNo() }}</td>
   <td>0.16{{ property_prefix("webkit") }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
   <td>{{ CompatNo() }}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="See_also">See also</h2>

<p>Specification:&nbsp;{{ spec("https://dev.w3.org/2009/dap/file-system/pub/FileSystem/", "File API: Directories and System Specification", "WD") }}</p>

<p>Reference: <a href="/en/DOM/File_API/File_System_API" title="en/DOM/File_API/File_System_API">File System API</a></p>

<p>Introduction:&nbsp;<a href="/en/DOM/File_APIs/Filesystem/Basic_Concepts_About_the_Filesystem_API" title="en/DOM/File_APIs/Filesystem/Basic_Concepts_About_the_Filesystem_API">Basic Concepts About the File System API</a></p>
Revert to this revision