4
const dirHandle = await window.showDirectoryPicker();
await dirHandle.requestPermission({ mode: "readwrite" });

I'm using the File System Access API in chrome. I'd like to let the user pick a folder, then write into the folder.

My code works, but two alerts are shown sequentially, one for read and one for write: enter image description here enter image description here

The first one is unnecessary. How can I avoid it?

Interestingly, if the user uses drag and drop, only the 2nd alert will appear after the folder is dropped, which is the desired behavior. The first alert seems to come from showDirectoryPicker. In the ideal world, I imagine being able to pass in an option like showDirectoryPicker({ permission: 'readwrite' }), which will request the 2 permissions together.

ZYinMD
  • 3,602
  • 1
  • 23
  • 32

2 Answers2

1

As of Chrome 105, you can get a writable directory with just one prompt

const dirHandle = await window.showDirectoryPicker({ mode: "readwrite" });

Or be explicit in asking for a read-only directory (which is the default).

const dirHandle = await window.showDirectoryPicker({ mode: "read" });
-1

I agree it feels suboptimal, but it's a one-time thing. When you run the same code again and pick the same folder (or a nested folder), there will be no prompts at all.

This design was chosen because there are two different things that are being asked here:

  • First, for the app to read all files (which, recursively for subfolders can be a lot).
  • Second, for the app to be allowed to write (anywhere) into the folder.
DenverCoder9
  • 2,024
  • 11
  • 32
  • 1
    Thanks for your answer, I have two problems: 1. it's not a one-time thing because every time the user reloads the page and pick the same folder, the two alerts show again. 2. if the user provides the folder by drag and drop, the first alert won't show, but the "two different things" you mentioned are still both achieved – ZYinMD Nov 25 '21 at 18:57
  • Yes, a reload will currently still show the prompts. There's thinking to weaken this, but nothing to announce quite yet. – DenverCoder9 Nov 29 '21 at 08:51
  • does it allow to write into subfolders? If not, will that feature come? – Chau Nguyen Jan 10 '23 at 05:35
  • If you have write access to a parent folder, then you also have write access to subfolders. – DenverCoder9 Jan 11 '23 at 08:50