Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid two sequential alerts (one for read and one for edit) when using `window.showDirectoryPicker()`

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.

like image 433
ZYinMD Avatar asked Jan 19 '26 20:01

ZYinMD


1 Answers

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" });
like image 167
Austin Sullivan Avatar answered Jan 21 '26 08:01

Austin Sullivan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!