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

FileSystemEntry: getParent() method

· 52CE 我爱测

The FileSystemEntry interface's method **`getParent()`** obtains a FileSystemDirectoryEntry.

File and Directory Entries API

The FileSystemEntry interface's method getParent() obtains a FileSystemDirectoryEntry.

Syntax

getParent(successCallback, errorCallback)
getParent(successCallback)

Parameters

  • successCallback
    • : A function which is called when the parent directory entry has been retrieved. The callback receives a single input parameter: a FileSystemDirectoryEntry object representing the parent directory. The parent of the root directory is considered to be the root directory, itself, so be sure to watch for that.
  • errorCallback
    • : An optional callback which is executed if an error occurs. There's a single parameter: a DOMException describing what went wrong.

Return value

None (undefined).

Exceptions

  • DOMException.INVALID_STATE_ERR
    • : The operation failed because the file system's state doesn't permit it. This can happen, for example, if the file system's cached state differs from the actual state of the file system.
  • DOMException.NOT_FOUND_ERR
    • : The specified path could not be found.
  • DOMException.SECURITY_ERR
    • : Security restrictions prohibit obtaining the parent directory's information.

Examples

This example renames the file specified by the variable fileEntry to "newname.html".

fileEntry.getParent(
  (parent) => {
    fileEntry.moveTo(parent, "newname.html", (updatedEntry) => {
      console.log(`File ${fileEntry.name} renamed to newname.html.`);
    });
  },
  (error) => {
    console.error(
      `An error occurred: Unable to rename ${fileEntry.name} to newname.html.`,
    );
  },
);

This is accomplished by first obtaining a FileSystemDirectoryEntry object representing the directory the file is currently located in. Then FileSystemEntry.moveTo is used to rename the file within that directory.

Using promises

Currently, there isn't a Promise-based version of this method. You can, however, create a simple helper function to adapt it, like this:

function getParentPromise(entry) {
  return new Promise((resolve, reject) => {
    entry.getParent(resolve, reject);
  });
}

A similar approach can be taken elsewhere in the File and Directory Entries API.

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

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

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