GoogleChromeLabs / browser-fs-access

File System Access API with legacy fallback in the browser
https://googlechromelabs.github.io/browser-fs-access/demo/
Apache License 2.0
1.37k stars 82 forks source link

can't get handle from directoryOpen function #111

Closed sytolk closed 2 years ago

sytolk commented 2 years ago

The File System Access API return handle

const dirHandle = await window.showDirectoryPicker();

but directoryOpen return files[] in selected directory:

const fsAccess = require("browser-fs-access");
  const blobsInDirectory = await fsAccess.directoryOpen({
    recursive: false,
  });

How can I get selected directory path only?

sytolk commented 2 years ago

Fixed get handle without using browser-fs-access

tomayac commented 2 years ago

You can obtain the handles from the returned files, see this demo and the relevant snippet below:

import { directoryOpen } from "https://unpkg.com/browser-fs-access";

document.querySelector("button").addEventListener("click", async () => {
  const files = await directoryOpen({ recursive: true });
  files.forEach((file) => {
    console.log("FileSystemFileHandle", file.handle);
    console.log("FileSystemDirectoryHandle", file.directoryHandle);
  });
});
Screen Shot 2022-05-04 at 10 45 13

This is for the directory structure below:

Screen Shot 2022-05-04 at 10 48 09