admin 管理员组文章数量: 1086019
I need to read all the files and directories of a folder opened with the new File System Access API for the web. I am able to read a directory but don't know how to continue recursively on an elegant way
try {
const directoryHandle = await window.showDirectoryPicker()
const files = []
for await (let [name, handle] of directoryHandle) {
const kind = handle.kind
files.push({
name,
handle,
kind,
})
}
I need to read all the files and directories of a folder opened with the new File System Access API for the web. I am able to read a directory but don't know how to continue recursively on an elegant way
try {
const directoryHandle = await window.showDirectoryPicker()
const files = []
for await (let [name, handle] of directoryHandle) {
const kind = handle.kind
files.push({
name,
handle,
kind,
})
}
Share
Improve this question
edited Feb 2 at 19:58
VLAZ
29.1k9 gold badges63 silver badges84 bronze badges
asked Oct 9, 2020 at 16:14
ctwhomectwhome
8341 gold badge15 silver badges28 bronze badges
1 Answer
Reset to default 12two steps, define one function that should take a directory and return all files and folders recursively as below
async function listAllFilesAndDirs(dirHandle) {
const files = [];
for await (let [name, handle] of dirHandle) {
const {kind} = handle;
if (handle.kind === 'directory') {
files.push({name, handle, kind});
files.push(...await listAllFilesAndDirs(handle));
} else {
files.push({name, handle, kind});
}
}
return files;
}
then invoke this function from your code as below
async function onClickHandler(e) {
try {
const directoryHandle = await window.showDirectoryPicker()
const files = await listAllFilesAndDirs(directoryHandle);
console.log('files', files);
}catch(e) {
console.log(e);
}
}
本文标签:
版权声明:本文标题:javascript - How to recursively read local files and directories in web browser using File System Access API - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744068120a2527985.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论