This is an experimental technologyCheck the Browser compatibility table carefully before using this in production.
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.
Syntax
FileSystemFileEntry.file(successCallback[, errorCallback]);
Parameters
successCallback- A callback function which is called when the
Filehas been created successfully; theFileis passed into the callback as the only parameter. errorCallbackOptional- If provided, this must be a method which is called when an error occurs while trying to create the
File. This callback receives as input aFileErrorobject describing the error.
Editor's note: We need to find out what kinds of errors can occur and document them.
Return value
Example
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.
Specifications
| Specification | Status | Comment |
|---|---|---|
| File and Directory Entries APIThe definition of 'file()' in that specification. | Draft | Initial specification. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
file
|
Chrome
Full support 8 |
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
See also
FileSystemFileEntry.file() by Mozilla Contributors is licensed under CC-BY-SA 2.5.