javascriptnative-file-system-api-js

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


I am struggling to understand if this is a bug or a feature of the new File System API. When using showDirectoryPicker and selecting the standard Windows documents directory, or the downloads directory, a pop-up appears stating that it can't open the folder because it contains system files, which it does not. Has anyone found a way around this, or am I missing something obvious?

enter image description here

Here is a fully functional example:

<!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>

Solution

  • By design the File System Access API excludes certain folders from being opened or limits access to them. They are listed as the so-called well-known directories in the spec. Concretely, they are:

    Typically, browsers also block access to system directories like C:\Windows.

    The reason these folders are blocked are (i), preventing access to system-critical files (a web application should not be able to wipe your Windows folder) and (ii) preventing files to be abused as identifiers (e.g., two independent apps which had access to the user's downloads folder could create and then check for the existence of an identifying file). See the relevant section of the spec for more details and background.