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.38k stars 84 forks source link

DOMException: The request is not allowed by the user agent or the platform in the current context #78

Closed siquylee closed 2 years ago

siquylee commented 2 years ago

fileSave works fine with Google Chrome but it threw an exception DOMException: The request is not allowed by the user agent or the platform in the current context when I ran with Microsoft Edge. Please advise.

siquylee commented 2 years ago

I solved it

tomayac commented 2 years ago

Do you want to share the solution for others who might come here? Thanks in advance if you can!

klintmane commented 2 years ago

Sharing a possible solution in here in case anyone else encounters this.

This error message is displayed when the browser loses file access permissions (in my case it was due to a page refresh - trying to access a previously stored file handle). I encountered it when calling fileHandle.getFile(), but it might happen under other circumstances - like in the OPs case (fileSave).

The solution is to re-grant file permissions like below:

// The return values are optional, but you can use them for tracking permission state
async function hasPermission(fileHandle) {
  if ((await fileHandle.queryPermission()) === "granted")  return true; // Permission already granted
  if ((await fileHandle.requestPermission()) === "granted") return true; // Prompt user for permission
  return false; // Permission denied
}

async function loadFileFromHandle(fileHandle) {
  const permitted = await hasPermission(fileHandle);
  return premitted ? fileHandle.getFile() : null;
}