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 408543 of DirectoryReaderSync

  • Revision slug: Web/API/DirectoryReaderSync
  • Revision title: DirectoryReaderSync
  • Revision id: 408543
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment 7 words removedDOM/File_API/File_System_API/DirectoryReaderSync Web/API/DirectoryReaderSync

Revision Content

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

About this document

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

Basic concepts

Before you call the only method in this interface, readEntries(), create the DirectoryEntrySync object. But DirectoryEntrySync (as well as FileEntrySync) is not a data type that you can pass between a calling app and Web Worker thread. It's not a big deal, because you don't really need to have the main app and the worker thread see the same JavaScript object; you just need them to access the same files. You can do that by passing a list of  filesystem: URLs—which are just strings—instead of a list of entries. You can also use the filesystem: URL to look up the entry with resolveLocalFileSystemURL(). That gets you back to a DirectoryEntrySync (as well as FileEntrySync) object.

Example

In the following code snippet from HTML5Rocks, we create Web Workers and pass data from it to the main app.

// Taking care of the browser-specific prefixes.
  window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL ||
                                     window.webkitResolveLocalFileSystemURL;

// Create web workers
  var worker = new Worker('worker.js');
  worker.onmessage = function(e) {
    var urls = e.data.entries;
    urls.forEach(function(url, i) {
      window.resolveLocalFileSystemURL(url, function(fileEntry) {
        // Print out file's name.
        console.log(fileEntry.name); 
      });
    });
  };

  worker.postMessage({'cmd': 'list'});

The following is Worker.js code that gets the contents of the directory.

// Taking care of the browser-specific prefixes.
self.requestFileSystemSync = self.webkitRequestFileSystemSync ||
                             self.requestFileSystemSync;

// Global for holding the list of entry file system URLs.
var paths = []; 

function getAllEntries(dirReader) {
  var entries = dirReader.readEntries();

  for (var i = 0, entry; entry = entries[i]; ++i) {
    // Stash this entry's filesystem in URL
    paths.push(entry.toURL()); 

    // If this is a directory, traverse.
    if (entry.isDirectory) {
      getAllEntries(entry.createReader());
    }
  }
}

// Forward the error to main app.
function onError(e) {
  postMessage('ERROR: ' + e.toString()); 
}

self.onmessage = function(e) {
  var data = e.data;

  // Ignore everything else except our 'list' command.
  if (!data.cmd || data.cmd != 'list') {
    return;
  }

  try {
    var fs = requestFileSystemSync(TEMPORARY, 1024*1024 /*1MB*/);

    getAllEntries(fs.root.createReader());

    self.postMessage({entries: paths});
  } catch (e) {
    onError(e);
  }
};

Method overview

EntrySync readEntries () raises (FileException);

Method

readEntries()

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

EntrySync readEntries (
) raises (FileException);
Returns
Parameter

None

Exceptions

This method can raise a FileException with the following codes:

Exception Description
NOT_FOUND_ERR The directory does not exist.
INVALID_STATE_ERR The directory has been modified since the first call to readEntries was processed.
SECURITY_ERR The browser determined that it was not safe to look up the metadata.

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>The <code>DirectoryReaderSync</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>
<h2 id="About_this_document">About this document</h2>
<p>This document was last updated on March 2, 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>
<h2 id="basic_concepts" name="basic_concepts">Basic concepts</h2>
<p>Before you call the only method in this interface, <a href="/#readEntries()" title="#readEntries()"><code>readEntries()</code></a>, create the <a href="/en/DOM/File_API/File_System_API/DirectoryEntrySync" title="https://developer.mozilla.org/en/DOM/File_API/File_System_API/DirectoryEntrySync"><code>DirectoryEntrySync</code></a> object. But DirectoryEntrySync (as well as <a href="/en/DOM/File_API/File_System_API/FileEntrySync" title="https://developer.mozilla.org/en/DOM/File_API/File_System_API/FileEntrySync">FileEntrySync</a>) is not a data type that you can pass between a calling app and Web Worker thread. It's not a big deal, because you don't really need to have the main app and the worker thread see the same JavaScript object; you just need them to access the same files. You can do that by passing a list of  <code>filesystem:</code> URLs—which are just strings—instead of a list of entries. You can also use the <code>filesystem:</code> URL to look up the entry with <a href="/en/DOM/File_API/File_System_API/LocalFileSystem#resolveLocalFileSystemURL()" title="https://developer.mozilla.org/en/DOM/File_API/File_System_API/LocalFileSystem#resolveLocalFileSystemURL()"><code>resolveLocalFileSystemURL()</code></a>. That gets you back to a DirectoryEntrySync (as well as FileEntrySync) object.</p>
<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 create Web Workers and pass data from it to the main app.</p>
<pre class="brush: js">// Taking care of the browser-specific prefixes.
  window.resolveLocalFileSystemURL = window.resolveLocalFileSystemURL ||
                                     window.webkitResolveLocalFileSystemURL;

// Create web workers
  var worker = new Worker('worker.js');
  worker.onmessage = function(e) {
    var urls = e.data.entries;
    urls.forEach(function(url, i) {
      window.resolveLocalFileSystemURL(url, function(fileEntry) {
        // Print out file's name.
        console.log(fileEntry.name); 
      });
    });
  };

  worker.postMessage({'cmd': 'list'});
</pre>
<p>The following is Worker.js code that gets the contents of the directory.</p>
<pre class="brush: js">// Taking care of the browser-specific prefixes.
self.requestFileSystemSync = self.webkitRequestFileSystemSync ||
                             self.requestFileSystemSync;

// Global for holding the list of entry file system URLs.
var paths = []; 

function getAllEntries(dirReader) {
  var entries = dirReader.readEntries();

  for (var i = 0, entry; entry = entries[i]; ++i) {
    // Stash this entry's filesystem in URL
    paths.push(entry.toURL()); 

    // If this is a directory, traverse.
    if (entry.isDirectory) {
      getAllEntries(entry.createReader());
    }
  }
}

// Forward the error to main app.
function onError(e) {
  postMessage('ERROR: ' + e.toString()); 
}

self.onmessage = function(e) {
  var data = e.data;

  // Ignore everything else except our 'list' command.
  if (!data.cmd || data.cmd != 'list') {
    return;
  }

  try {
    var fs = requestFileSystemSync(TEMPORARY, 1024*1024 /*1MB*/);

    getAllEntries(fs.root.createReader());

    self.postMessage({entries: paths});
  } catch (e) {
    onError(e);
  }
};
</pre>
<h2 id="Method_overview">Method overview</h2>
<table class="standard-table"> <tbody> <tr> <td><code>EntrySync <a href="#createReader" title="#readEntries">readEntries</a> () raises (<a href="/en/DOM/File_API/File_System_API/FileException" title="en/DOM/File_API/File_System_API/FileException">FileException</a>);</code></td> </tr> </tbody>
</table>
<h2 id="Method">Method</h2>
<h3 id="readEntries" name="readEntries">readEntries()</h3>
<p>Returns a lost of entries from a specific directory. Call this method until an empty array is returned.</p>
<pre>EntrySync readEntries (
) raises (<a href="https://developer.mozilla.org/en/DOM/File_API/File_System_API/FileException">FileException</a>);</pre>
<h5 id="Returns">Returns</h5>
<h5 id="Parameter">Parameter</h5>
<p>None</p>
<h5 id="Exceptions">Exceptions</h5>
<p>This method can raise a <a href="/en/DOM/File_API/File_System_API/FileException" title="en/DOM/File_API/File_System_API/FileException">FileException</a> with the following codes:</p>
<table class="standard-table"> <thead> <tr> <th scope="col" width="131">Exception</th> <th scope="col" width="698">Description</th> </tr> <tr> <td><code>NOT_FOUND_ERR</code></td> <td>The directory does not exist.</td> </tr> </thead> <tbody> <tr> <td><code>INVALID_STATE_ERR</code></td> <td>The directory has been modified since the first call to readEntries was processed.</td> </tr> <tr> <td><code>SECURITY_ERR</code></td> <td>The browser determined that it was not safe to look up the metadata.</td> </tr> </tbody>
</table>
<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 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: {{ 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: <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