javascript - Accessing sub-directory file content using showDirectoryPicker() - Stack Overflow

Using the File System Access API, how would I access the files contained within a folder of the chosen

Using the File System Access API, how would I access the files contained within a folder of the chosen directory?

document.querySelector('button').addEventListener('click', async () => {
  const dirHandle = await window.showDirectoryPicker();
  for await (const entry of dirHandle.values()) {
    if (entry.kind === "file"){
      const file = await entry.getFile();
      const text = await file.text();
      console.log(text);
    }
    if (entry.kind === "directory"){
      /* for file in this directory do something */ 
    }
  }
});
<button>Choose Directory</button>

Using the File System Access API, how would I access the files contained within a folder of the chosen directory?

document.querySelector('button').addEventListener('click', async () => {
  const dirHandle = await window.showDirectoryPicker();
  for await (const entry of dirHandle.values()) {
    if (entry.kind === "file"){
      const file = await entry.getFile();
      const text = await file.text();
      console.log(text);
    }
    if (entry.kind === "directory"){
      /* for file in this directory do something */ 
    }
  }
});
<button>Choose Directory</button>

Share Improve this question edited Jun 15, 2021 at 6:00 Kaiido 138k14 gold badges258 silver badges324 bronze badges asked Jun 15, 2021 at 4:01 Conrad KlekConrad Klek 1431 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

A small improvement to Kaiido's answer:

btn.onclick = async (evt) => {
  const out = {};
  const dirHandle = await showDirectoryPicker();  
  await handleDirectoryEntry( dirHandle, out );
  console.log( out );
};
async function handleDirectoryEntry( dirHandle, out ) {
  for await (const entry of dirHandle.values()) {
    if (entry.kind === "file"){
      const file = await entry.getFile();
      out[ file.name ] = file;
    }
    if (entry.kind === "directory") {
      const newOut = out[ entry.name ] = {};
      await handleDirectoryEntry( entry, newOut );
    }
  }
}

dirHandle.values() returns a list of objects that inherit from FileSystemHandle, there are two possibilities: either FileSystemFileHandle or FileSystemDirectoryHandle.

Since const entry already is a FileSystemDirectoryHandle in case when entry.kind is "directory" there is no need to call dirHandle.getDirectoryHandle()

You need to call the dirHandle.getDirectoryHandle(name, options) method, with the name parameter set to your entry's .name.

Here is an example code that will walk through a passed directory and build a tree of the files it found.

btn.onclick = async (evt) => {
  const out = {};
  const dirHandle = await showDirectoryPicker();  
  await handleDirectoryEntry( dirHandle, out );
  console.log( out );
};
async function handleDirectoryEntry( dirHandle, out ) {
  for await (const entry of dirHandle.values()) {
    if (entry.kind === "file"){
      const file = await entry.getFile();
      out[ file.name ] = file;
    }
    if (entry.kind === "directory") {
      const newHandle = await dirHandle.getDirectoryHandle( entry.name, { create: false } );
      const newOut = out[ entry.name ] = {};
      await handleDirectoryEntry( newHandle, newOut );
    }
  }
}

Live demo, edit code.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743763418a4503054.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信