Web/API/FileSystemDirectoryEntry/createReader

From Get docs

This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.


The FileSystemDirectoryEntry interface's method createReader() returns a FileSystemDirectoryReader object which can be used to read the entries in the directory.

Syntax

directoryReader = FileSystemDirectoryEntry.createReader();

Parameters

None.

Return value

A FileSystemDirectoryReader object which can be used to read the directory's entries.

Example

This example creates a method called readDirectory(), which fetches all of the entries in the specified FileSystemDirectoryEntry and returns them in an array.

function readDirectory(directory) {
  let dirReader = directory.createReader();
  let entries = [];

  let getEntries = function() {
    dirReader.readEntries(function(results) {
      if (results.length) {
        entries = entries.concat(toArray(results));
        getEntries();
      }
    }, function(error) {
      /* handle error -- error is a FileError object */
    });
  };

  getEntries();
  return entries;
}

This works by creating an internal function, getEntries(), which calls itself recursively to get all the entries in the directory, concatenating each batch to the array. Each iteration, readEntries() is called to get more entries. When it returns an empty array, the end of the directory has beenr reached, and the recursion ends. Once control is returned to readDirectory(), the array is returned to the caller.

Specifications

Specification Status Comment
File and Directory Entries APIThe definition of 'createReader()' in that specification. Draft Initial specification.

Browser compatibility

Update compatibility data on GitHub

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet

createReader

Experimental'

Chrome

Full support 13

Edge

Full support 79

Firefox

Full support 50

IE

No support No

Opera

No support No

Safari

Full support 11.1

WebView Android

Full support ≤37

Chrome Android

Full support 18

Firefox Android

Full support 50

Opera Android

No support No

Safari iOS

Full support 11.3

Samsung Internet Android

Full support 1.0

Legend

Full support  
Full support
No support  
No support
Experimental. Expect behavior to change in the future.'
Experimental. Expect behavior to change in the future.


See also