javascriptfile-system-access-api

How do you get showDirectoryPicker(); from File System Access Api to not ask for permission even when you store dirHandle in IndexDB


When you call showDirectoryPicker(); you get a directory picker. After you select a directory chrome will ask you for permission to view files in that folder.

Example code:

 let  dirHandle = showDirectoryPicker();

The above code will ask for permissions every time you choose a folder even if you have already given access to it.

You should be able to store dirHandle in indexDB so that if a user has already given permission then it will not ask for permissions again if you use the already saved handle from indexDB and you choose a folder you have already granted permissions.

The problem is that I am saving and retrieving the dirHandle to/from indexDb but it still asks for permission on folders I have already given permission to.

Code I thought should work: Note -- get and set are just shorthand for saving and loading to indexDb

asyn function (){
 let dirHandle = await get("directory");
 dirHandle = await window.showDirectoryPicker();
  await set("directory", dirHandle);
}

So in the above code I retrieve dirHand from indexDb and then set that to dirHand = window.ShowDirectoryPicker. A window will pop up asking you to select a folder. If I select a folder I have already given permissions to it should not ask for permissions again even after page refresh. It works fine if you do not refresh the page.

I am pretty sure this should work as seen in this example project https://github.com/GoogleChromeLabs/text-editor/

This project is from this nice article explaining how to achieve these results but obviously it is not working for me. See article https://web.dev/file-system-access.

You can try live his text editor demonstrating that you will not get asked for permissions from a previously granted folder even after page refresh. See live demo https://googlechromelabs.github.io/text-editor/

Example go to File > Open and choose a file. If this is the first time opening any file in this folder it will ask you for permissions to view files. Now refresh the page and try to open the same file. It will not ask for pretermission again and you can save the file. It will ask for permissions the first time you save a file but not on subsequent calls. I have looked at his code and I cannot figure out what I am doing wrong. Any help is greatly appreciated. TY


Solution

  • Chrome always asks for permission, even for persisted file handles on IndexedDB:

    The initial prompt:

    enter image description here

    The prompt after you re-open a file from a persisted file handle:

    enter image description here