FileSystemEntry.getParent() - Web APIs 编辑

Experimental

This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

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

Syntax

FileSystemEntry.getParent(successCallback[, errorCallback]);

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 Optional
An optional callback which is executed if an error occurs. There's a single parameter: a FileError describing what went wrong.

Return value

undefined.

Errors

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.

Example

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.

Specifications

SpecificationStatusComment
File and Directory Entries API
The definition of 'getParent()' in that specification.
DraftInitial specification.

Browser compatibility

BCD tables only load in the browser

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:140 次

字数:5120

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文