Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. Web APIs
  3. FileSystemEntry
  4. getParent()

FileSystemEntry: getParent() method

TheFileSystemEntry interface's methodgetParent() obtains aFileSystemDirectoryEntry.

Syntax

js
getParent(successCallback, errorCallback)getParent(successCallback)

Parameters

successCallback

A function which is called when the parent directory entry has been retrieved. Thecallback receives a single input parameter: aFileSystemDirectoryEntryobject representing the parent directory. The parent of the root directory isconsidered to be the root directory, itself, so be sure to watch for that.

errorCallbackOptional

An optional callback which is executed if an error occurs. There's a singleparameter: aDOMException 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 canhappen, for example, if the file system's cached state differs from the actual stateof 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 variablefileEntry to"newname.html".

js
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 aFileSystemDirectoryEntryobject representing the directory the file is currently located in. ThenmoveTo() is used to rename the file within thatdirectory.

Using promises

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

js
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

Specification
File and Directory Entries API
# dom-filesystementry-getparent

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp