FileSystemDirectoryEntry.getFile()
The FileSystemDirectoryEntry interface's method getFile() returns a FileSystemFileEntry object corresponding to a file contained somewhere within the directory subtree rooted at the directory on which it's called.
Syntax
FileSystemDirectoryEntry.getFile([path][, options][, successCallback][, errorCallback]);
Parameters
-
pathOptional -
A
USVStringspecifying the path, relative to the directory on which the method is called, describing which file's entry to return. -
optionsOptional -
An object based on the
FileSystemFlagsdictionary, which allows you to specify whether or not to create the entry if it's missing and if it's an error if the file already exists. These options are currently not useful in Web contexts. -
successCallbackOptional -
A method to be called once the
FileSystemFileEntryhas been created. The method receives a single parameter: theFileSystemFileEntryobject representing the file in question. -
errorCallbackOptional -
A method to be called if an error occurs. Receives as its sole input parameter a
DOMExceptionobject describing the error which occurred.
Return value
None.
Exceptions
-
NotFoundErrorDOMException -
Thrown if the
createoption was not specified (or was specified asfalse), and the file doesn't exist. -
SecurityErrorDOMException -
Thrown if the request to access the file was denied for security reasons.
-
TypeMismatchErrorDOMException -
Thrown if the path specified is not a file; it's probably a directory, but might be an unsupported file descriptor such as a pipe; this depends on the user agent to some extent.
Example
In this example, a function is presented whose job it is to locate within a user's app data directory a JSON file containing a user dictionary for a specified language, then load that dictionary.
let dictionary = null; function loadDictionaryForLanguage(appDataDirEntry, lang) { dictionary = null; appDataDirEntry.getDirectory("Dictionaries", {}, function(dirEntry) { dirEntry.getFile(lang + "-dict.json", {}, function(fileEntry) { fileEntry.file(function(dictFile) { let reader = new FileReader(); reader.addEventListener("loadend", function() { dictionary = JSON.parse(reader.result); }); reader.readAsText(dictFile); }); }); }); }
The loadDictionaryForLanguage() function starts by using getDirectory() to obtain the FileSystemDirectoryEntry object representing a subfolder named "Dictionaries" located inside the specified app data directory. The success callback for this takes the resulting directory entry object and calls getFile() to get a FileSystemFileEntry object representing the dictionary file; the success callback for this, in turn, creates a new FileReader and uses it to load the contents of the file. When that is loaded successfully (as indicated by the loadend event being fired), the loaded text is passed into JSON.parse() to be reconstituted into a JavaScript object.
Specifications
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
getFile |
8 |
79 |
50
|
No |
No |
11.1 |
≤37 |
18 |
50
|
No |
11.3 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/FileSystemDirectoryEntry/getFile