这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

FileSystemEntry 接口方法 getParent () obtains a FileSystemDirectoryEntry .

句法

FileSystemEntry.getParent(successCallback[, errorCallback]);
					

参数

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 FileError describing what went wrong.

返回值

undefined .

错误

FileError.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.

FileError.NOT_FOUND_ERR

The specified path could not be found.

FileError.SECURITY_ERR

Security restrictions prohibit obtaining the parent directory's information.

范例

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

fileEntry.getParent(function(parent) {
  fileEntry.moveTo(parent, "newname.html", function(updatedEntry) {
    console.log("File " + fileEntry.name + " renamed to newname.html.");
  });
}, function(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 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.

规范

规范 状态 注释
文件和目录条目 API
The definition of 'getParent()' in that specification.
草案 最初的规范。

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
getParent Chrome 8 Edge 79 Firefox No IE No Opera No Safari 11.1 WebView Android ≤37 Chrome Android 18 Firefox Android No Opera Android No Safari iOS 11.3 Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: