大网数据广告活动
Web 标准与前端开发

FileSystemDirectoryEntry: getDirectory() method

· 52CE 我爱测

The FileSystemDirectoryEntry interface's method **`getDirectory()`** returns a FileSystemDirectoryEntry object corresponding to a directory contained somewhere within the directory subtree rooted at the directory on which it's called.

File and Directory Entries API

The FileSystemDirectoryEntry interface's method getDirectory() returns a FileSystemDirectoryEntry object corresponding to a directory contained somewhere within the directory subtree rooted at the directory on which it's called.

Syntax

getDirectory()
getDirectory(path)
getDirectory(path, options)
getDirectory(path, options, successCallback)
getDirectory(path, options, successCallback, errorCallback)

Parameters

  • path
    • : A string representing an absolute path or a path relative to the directory on which the method is called, describing which directory entry to return. Absolute paths may not be able to be used, for security reasons.
  • options
    • : An object 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. See the options parameter section for more details.
  • successCallback
    • : A method to be called once the FileSystemDirectoryEntry has been created. The method receives a single parameter: the FileSystemDirectoryEntry object representing the directory in question.
  • errorCallback
    • : A method to be called if an error occurs. Receives as its sole input parameter a DomException object describing the error which occurred.

options parameter

The options parameter object accepts the following parameters:

  • create
    • : If this property is true, and the requested directory doesn't exist, the user agent should create it. The default is false. The parent directory must already exist.
  • exclusive
    • : If true, and the create option is also true, the directory must not exist prior to issuing the call. Instead, it must be possible for it to be created newly at call time. The default is false. This parameter is ignored if create is false.

The table below describes the result of each possible combination of these flags depending on whether or not the target directory path already exists.

create option exclusive option Path condition Result
false Ignored Path exists and is a directory The successCallback is called with a FileSystemDirectoryEntry.
false Ignored Path exists but is a file The errorCallback is called with an appropriate error code (if the callback was provided).
true false Path exists The existing directory is removed and replaced with a new one, then the successCallback is called with a FileSystemDirectoryEntry.
true false Path doesn't exist The directory is created, then a FileSystemDirectoryEntry is passed to the successCallback.
true true Path exists The errorCallback is called with an appropriate error, such as DOMException.PATH_EXISTS_ERR.
true true Path doesn't exist The directory is created, then a FileSystemDirectoryEntry is passed to the successCallback.

Return value

None (undefined).

Exceptions

  • NotFoundError DOMException
    • : Thrown if the create option was not specified (or was specified as false), and the directory doesn't exist.
  • SecurityError DOMException
    • : Thrown if the request to access the directory was denied for security reasons.
  • TypeMismatchError DOMException
    • : Thrown if the path specified is not a directory; it's probably a file, but might be an unsupported file descriptor such as a pipe; this depends on the user agent to some extent.

Examples

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", {}, (dirEntry) => {
    dirEntry.getFile(`${lang}-dict.json`, {}, (fileEntry) => {
      fileEntry.file((dictFile) => {
        let reader = new FileReader();

        reader.addEventListener("loadend", () => {
          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 FileSystemDirectoryEntry.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 FileReader/loadend_event event being fired), the loaded text is passed into JSON.parse() to be reconstituted into a JavaScript object.

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。