我的网站需要打开本地文件功能,因此我在代码中使用文件系统访问 API。 当从本地打开的文件被我的网站编辑并需要保存到原始文件时。我发现没有权限。因此,我找到了请求文件句柄权限的方法,如下所示:
async function verifyPermission(fileHandle: any, readWrite: boolean) {
const options = {
mode: 'readwrite',
};
let isPermit = false;
// Check if permission was already granted. If so, return true.
if ((await fileHandle.queryPermission(options)) === 'granted') {
isPermit = true;
}
if ((await fileHandle.requestPermission(options)) === 'granted') {
isPermit = true;
}
return isPermit;
}
但是当我执行fileHandle.requestPermission(options)
时,它控制台错误
Uncaught (in promise) DOMException: User activation is required to request permissions.
我缺少什么?谢谢QQ
最佳答案
您始终可以查询文件的权限,但请求需要用户手势(例如单击按钮)。 spec 中对此进行了概述。 (强调我的):
If the state of the read permission of this handle is anything other than "prompt", this will return that state directly. If it is "prompt" however, user activation is needed and this will show a confirmation prompt to the user. The new read permission state is then returned, depending on the user’s response to the prompt.
您需要在实现中解耦查询和请求权限。
关于javascript - FileSystemHandle.requestPermission DOMException : User activation is required to request permissions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69732680/