javascript - FileSystemHandle.requestPermission DOMException : User activation is required to request permissions

标签 javascript filehandle file-system-access-api

我的网站需要打开本地文件功能,因此我在代码中使用文件系统访问 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/

相关文章:

perl - 当我想打印到哈希键中的句柄时,为什么会出现语法错误?

file-system-access-api - DOM 异常 : The request is not allowed by the user agent or the platform in the current context

javascript - 对象重映射问题 - 属性未定义

c# - 设置 Html 复选框从代码隐藏检查,无需 runat=server

javascript - 如何访问用javascript动态生成的textarea的文本内容?

perl - 如何在 Perl 类中存储和访问文件句柄?

javascript - 多列 HTML 列表框

perl - 打开文件句柄或分配标准输出

javascript - 在 Chrome 扩展中保留 FileSystemDirectoryHandle 对象