FileSystemDirectoryHandle:resolve() 方法

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

FileSystemDirectoryHandle 接口的 resolve() 方法返回一个 Promise 对象,会兑现一个包含从父目录前往指定子条目中间的目录的名称的数组。数组的最后一项是子条目的名称。

语法

js
resolve(possibleDescendant)

参数

possibleDescendant

要返回其相对路径的 FileSystemHandle

返回值

一个 Promise 对象,会兑现一个包含字符串的数组,或者当参数 possibleDescendant 不是此 FileSystemDirectoryHandle 的后代时,兑现 null

异常

不抛出异常。

示例

下面的异步函数使用 resolve() 来查找被选择文件相对于指定目录句柄的路径。

js
async function returnPathDirectories(directoryHandle) {
  // 通过显示文件选择器来获得一个文件句柄
  const [handle] = await self.showOpenFilePicker();
  if (!handle) {
    // 如果用户取消了选择或者打开文件失败
    return;
  }

  // 检查文件句柄是否存在于目录句柄的目录中
  const relativePaths = await directoryHandle.resolve(handle);

  if (relativePaths === null) {
    // 不在目录句柄中
  } else {
    // relativePaths 是一个包含名称的数组,指示相对路径
    for (const name of relativePaths) {
      // 打印数组的每个元素
      console.log(name);
    }
  }
}

规范

Specification
File System Standard
# api-filesystemdirectoryhandle-resolve

浏览器兼容性

BCD tables only load in the browser

参见