{{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:
- Call {{domxref("FileSystemDirectoryEntry.createReader()")}} to create a new
FileSystemDirectoryReader
. - Call {{domxref("FileSystemDirectoryReader.readEntries", "readEntries()")}}.
- 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
- File and Directory Entries API
- Introduction to the File System API
- {{domxref("FileSystemDirectoryEntry")}}
- {{domxref("FileSystem")}}