文件系统 API 声明它无法打开文档文件夹,因为它包含系统文件,但实际上并没有

File System API states it can't open the documents folder because it contains system files, which it doesn't

我很难理解这是新文件系统 API 的错误还是功能。当使用 showDirectoryPicker 并选择标准 Windows 文档目录或下载目录时,会出现一个弹出窗口,指出它无法打开该文件夹,因为它包含系统文件,而事实并非如此。有没有人找到解决这个问题的方法,或者我是否遗漏了一些明显的东西?

这是一个功能齐全的示例:

<!DOCTYPE HTML>
<html>
<head>
  <title>File System API</title>
</head>
<button onclick="checkDirExists();">Initialize</button>
<body>

</body>
<script>

async function checkDirExists(){
    const homeDirHdl = await window.showDirectoryPicker({
        startIn: 'documents',
    });
    if (homeDirHdl){
        const draftsDirHdl = await homeDirHdl.getDirectoryHandle('drafts', {create: true});
    }
}
</script>
</html>

根据设计,文件系统访问 API 禁止某些文件夹打开或限制对它们的访问。它们在规范中被列为 so-called well-known directories。具体是:

  • desktop:用户的桌面目录,如果有的话。例如,这可能是 C:\Documents and Settings\username\Desktop/Users/username/Desktop/home/username/Desktop.
  • documents:通常存储用户创建的文档的目录。例如 C:\Documents and Settings\username\My Documents/Users/username/Documents/home/username/Documents.
  • downloads:通常存储下载文件的目录。例如 C:\Documents and Settings\username\Downloads/Users/username/Downloads/home/username/Downloads.
  • music:通常存储音频文件的目录。例如 C:\Documents and Settings\username\My Documents\My Music/Users/username/Music/home/username/Music.
  • pictures:通常用于存储照片和其他静止图像的目录。例如 C:\Documents and Settings\username\My Documents\My Pictures/Users/username/Pictures/home/username/Pictures.
  • videos:通常存储 videos/movies 的目录。例如 C:\Documents and Settings\username\My Documents\My Videos/Users/username/Movies/home/username/Videos.

通常,浏览器还会阻止访问 系统 目录,例如 C:\Windows.

这些文件夹被阻止的原因是 (i),阻止访问 system-critical 文件(网络应用程序应该无法擦除您的 Windows 文件夹)和 (ii) 防止文件被滥用为标识符(例如,两个可以访问用户下载文件夹的独立应用程序可以创建然后检查识别文件是否存在)。有关详细信息和背景,请参阅 spec 的相关部分。