This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.
The FileSystemFileEntry interface's method file() returns a File object which can be used to read data from the file represented by the directory entry.
FileSystemFileEntry.file(successCallback[, errorCallback]);
successCallbackFile has been created successfully; the File is passed into the callback as the only parameter.errorCallback Optional
File. This callback receives as input a FileError object describing the error.Editor's note: We need to find out what kinds of errors can occur and document them.
This example establishes a method, readFile(), reads a text file and calls a specified callback function with the received text (in a String object) once the read is completed. If an error occurs, a specified (optional) error callback is called.
function readFile(entry, successCallback, errorCallback) {
entry.file(function(file) {
let reader = new FileReader();
reader.onload = function() {
successCallback(reader.result);
};
reader.onerror = function() {
errorCallback(reader.error);
}
reader.readAsText(file);
}, errorCallback);
}
This function calls file(), specifying as its success callback a method which proceeds to use a FileReader to read the file as text. The FileReader's load event handler is set up to deliver the loaded string to the successCallback specified when the readFile() method was called; similarly, its error handler is set up to call the errorCallback specified.
| Specification | Status | Comment |
|---|---|---|
| File and Directory Entries API The definition of 'file()' in that specification. | Draft | Initial specification. |
This API has no official W3C or WHATWG specification.
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 13 | No | 50 | No | No | No |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | Yes | Yes | No | 50 | No | No | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file