如何通过FileSystemFileHandle获取文件所在的FileSystemDirectoryHandle?
如何通过文件句柄获取父目录句柄?
可行,你可以直接利用已有的FileSystemFileHandle实例的getParent()方法来获取其所在父文件夹的FileSystemDirectoryHandle,无需额外调用showDirectoryPicker()弹窗。
具体实现代码
基于你提供的文件选择代码,只需添加一行调用getParent()的逻辑:
const [fileHandle] = await (window as any).showOpenFilePicker({ types: [ { description: 'ABCD Files', accept: { 'application/vnd.yourapp.abcd': ['.abcd'], }, }, ], }); // 获取父目录句柄 const parentDirectoryHandle = await fileHandle.getParent();
说明
getParent()是FileSystem API原生支持的方法,调用后直接返回文件所在父目录的句柄,不需要用户再次手动选择目录。- 权限层面:用户通过
showOpenFilePicker选择文件时,已授予该文件的访问权限,getParent()会自动继承这一权限,无需额外授权弹窗。
内容的提问来源于stack exchange,提问作者CraZyDroiD
相关产品推荐
相关产品推荐

