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.
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.
js
file(successCallback) file(successCallback, errorCallback)
successCallback
A callback function which is called when the File
has been created successfully; the File
is passed into the callback as the only parameter.
errorCallback
Optional
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 a DOMException
object describing the error.
None (undefined
).
This example establishes a method, readFile()
, reads a text file and calls a specified callback function with the received text (in a string) once the read is completed. If an error occurs, a specified (optional) error callback is called.
js
function readFile(entry, successCallback, errorCallback) { entry.file((file) => { let reader = new FileReader(); reader.onload = () => { successCallback(reader.result); }; reader.onerror = () => { 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.
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
file |
8 | 79 | 50 | No | No | 11.1 | ≤37 | 18 | 50 | 14 | 11.3 | 1.0 |
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileEntry/file