FileSystemDirectoryHandle:resolve() 方法
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2023年3月.
备注: 此特性在 Web Worker 中可用。
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> # api-filesystemdirectoryhandle-resolve> |