0

I try to add folder scanning into my web page. Folder selection works fine and I get prompted if I want to allow the access to selected folder. But then nothing happens. In console I see "1" as the last log entry, so await never returns.

Any ideas what I should do?

jQuery(document).ready(function ($) {
  const butDir = document.getElementById('butDirectory');
  butDir.addEventListener('click', async() => {
    console.log("1");
    const dirHandle = await window.showDirectoryPicker();
    console.log("2");
    const promises = [];
    for await (const entry of dirHandle.values()) {
      if (entry.kind !== 'file') {
        continue;
      }
      promises.push(entry.getFile().then((file) => `${file.name} (${file.size})`));
    }
    console.log(await Promise.all(promises));
  });
});
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Riho
  • 4,523
  • 3
  • 33
  • 48

2 Answers2

1

OK, got it working. Instead of having the code inside jquery.ready() I had to move it outside:

async function getDir() {
  const dirHandle = await window.showDirectoryPicker();
}

and call it directly from HTML

<input type="button" ... onclick="javascript:getDir();"/>
Riho
  • 4,523
  • 3
  • 33
  • 48
0

The documentation tells us that transient user activation is needed.

enter image description here. It also needs to be opened in its own tab, see Window.showDirectoryPicker() not working in Google Chrome 107

So, you will need to comply to the terms the documentation asks you to comply to and if the problem is still unresolved, then you will need to edit your question, provide more information and ping me here in the comment section.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • The reference to another issue with Chrome 107 does not apply, IMHO, as for me the directory picker window opens and I can select the folder. Also there is user activity on the page, as user presses the button in order to initiate the folder selection – Riho Nov 03 '22 at 14:45
  • @Riho understood. The information you have given in the above was not available for me yesterday, this is why I have written the answer the way I did. Still, in view of your answer it still seems that the browser does not recognize the fact that it is an even triggered by the user. – Lajos Arpad Nov 04 '22 at 08:56