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 1119353 of FileSystemDirectoryReader

  • Revision slug: Web/API/FileSystemDirectoryReader
  • Revision title: FileSystemDirectoryReader
  • Revision id: 1119353
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Updates for Firefox 50 support; still needs cleanup work

Revision Content

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

The FileSystemDirectoryReader interface of the File and Directory Entries API lets you read the entries in a directory.

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 {{domxref("FileSystemDirectoryEntry.createReader", "createReader()")}} to create a new FileSystemDirectoryReader.
  2. Call {{domxref("FileSystemDirectoryEntry.readEntries", "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

Specifications

Specification Status Comment
{{SpecName('File System API')}} {{Spec2('File System API')}} Draft of proposed API

This API has no official W3C or WHATWG specification.

Browser compatibility

{{ CompatibilityTable }}

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

See also

Revision Source

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

<p>The <code>FileSystemDirectoryReader</code> interface of the <a href="/en-US/docs/Web/API/File_and_Directory_Entries_API">File and Directory Entries API</a> lets you read the entries in a directory.</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 {{domxref("FileSystemDirectoryEntry.createReader", "createReader()")}} to create a new <code>FileSystemDirectoryReader</code>.</li>
 <li>Call {{domxref("FileSystemDirectoryEntry.readEntries", "readEntries()")}}.</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>

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

<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="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('File System API')}}</td>
   <td>{{Spec2('File System API')}}</td>
   <td>Draft of proposed API</td>
  </tr>
 </tbody>
</table>

<p>This API has no official W3C or WHATWG specification.</p>

<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>{{ CompatGeckoDesktop(50) }}</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>{{ CompatVersionUnknown }}&nbsp;{{ property_prefix("webkit") }}</td>
   <td>{{ CompatGeckoMobile(50) }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
   <td>{{ CompatNo }}</td>
  </tr>
 </tbody>
</table>
</div>

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

<ul>
 <li><a href="/en-US/docs/Web/API/File_and_Directory_Entries_API">File and Directory Entries API</a></li>
 <li><a href="/en-US/docs/Web/API/File_and_Directory_Entries_API/Introduction">Introduction to the File System API</a></li>
 <li>{{domxref("FileSystemDirectoryEntry")}}</li>
 <li>{{domxref("FileSystem")}}</li>
</ul>
Revert to this revision