【问题标题】:How do I use the File System Access API from Chrome using file:// url with no http server如何在没有 http 服务器的情况下使用文件:// url 从 Chrome 使用文件系统访问 API
【发布时间】:2020-09-30 01:31:43
【问题描述】:

我想在我的计算机上从 Chrome 打开一个 html 文件,没有 http 服务器,并且我希望 HTML 文件中的 JavaScript 能够读取和写入本地文件系统中的文件,以及浏览目录。

如何使用文件系统 API:https://wicg.github.io/file-system-access/ 执行此操作?

【问题讨论】:

    标签: file filesystems html5-filesystem


    【解决方案1】:

    文件系统 API 目前在 Chrome 85 中不可用。现在您可以使用批处理文件启动 html 文件,该批处理文件将使用正确的适当命令行选项定位并启动 Chrome。

    将批处理文件命名为与html文件相同的名称,并将以下内容放入批处理文件中:

    @echo off
    
    setlocal
    set name=%~n0
    set here=%~dp0
    
    cd /d %here%
    set indexFile=%here%%name%.html
    if not exist "%indexFile%" set indexFile=%here%%name%.htm
    if not exist "%indexFile%" Echo Could not locate "%name%.htm" or "%name%.html" & pause & goto :eof
    
    get path to msedge.exe
    set exe=
    FOR /F "tokens=2* skip=2" %%a in ('reg query HKCR\MSEdgeHTM\DefaultIcon /ve') do set exe=%%b
    cls
    set exe=%exe:~0,-2%
    if defined exe goto exeFound
    
    rem get path to chrome.exe
    set exe=
    FOR /F "tokens=2* skip=2" %%a in ('reg query HKCR\ChromeHTML\DefaultIcon /ve') do set exe=%%b
    cls
    set exe=%exe:~0,-2%
    if defined exe goto exeFound
    
    start "" "%indexFile%"
    goto :eof
    
    :exeFound
    start "" "%exe%" --enable-experimental-web-platform-features --disable-web-security --no-proxy-server --no-sandbox --allow-file-access-from-files --allow-file-access  --no-default-browser-check --no-first-run --allow-running-insecure-content --enable-local-file-accesses --disable-extensions --user-data-dir="%temp%\%name%" --app="file:///%indexFile%"
    

    在javascript中你可以这样调用:

    确定 API 是否可用

    if (typeof showDirectoryPicker === 'undefined')
    

    访问目录或文件

    const directoryHandle = await showDirectoryPicker()
    
    const fileHandle = await directoryHandle.getFileHandle(fileName)
    
    const str = await (await fileHandle.getFile()).text();
    

    请参阅 showOpenFilePicker()、showSaveFilePicker() 和 showDirectoryPicker() https://wicg.github.io/file-system-access/ 了解更多。

    更新:

    使用 Chrome 88 测试,不再需要批处理文件。

    更新 2:

    如何在针对文件启动 Chrome 或 Edge 时禁用网络安全:基于 url,来自 c#:

    private static void LaunchBrowser(string name, string indexFilePath)
    {
        var exe = GetBrowserExePath();
        if (string.IsNullOrEmpty(exe))
        {
            Console.WriteLine("Could not locate browser");
            return;
        }
    
        var userDataDir = Path.Combine(Path.GetTempPath(), name);
        var indexFile = new Uri(indexFilePath).AbsoluteUri;
    
        var parameters =
            " --disable-web-security" +
            " --no-default-browser-check" +
            " --no-first-run" +
            " --disable-extensions" +
            " --user-data-dir=" + "\"" + userDataDir + "\"" +
            " --app=" + "\"" + indexFile + "\"" +
            "";
        Process.Start(exe, parameters);
    }
    
    private static string GetBrowserExePath()
    {
        string exe = GetBrowserExePathWithKey("MdSEdgeHTM");
        if (string.IsNullOrEmpty(exe))
        {
            exe = GetBrowserExePathWithKey("ChromeHTML");
        }
    
        return exe;
    
        string GetBrowserExePathWithKey(string keyForChromeBaseBrowser)
        {
            var key = Registry.ClassesRoot.OpenSubKey(keyForChromeBaseBrowser);
            if (key != null)
            {
                key = key.OpenSubKey("DefaultIcon");
                if (key != null)
                {
                    var v = key.GetValue(string.Empty) as string;
                    var y = Regex.Match(v, @"(.*\.exe),");
                    if (y.Success)
                    {
                        var g = y.Groups;
                        if (g.Count > 1)
                        {
                            return g[1].Value;
                        }
                    }
                }
            }
    
            return string.Empty;
        }
    }
    
    

    【讨论】:

    • 请注意,它现在可以在 Chrome 86 中使用。我正在开发一个可能利用它的扩展...
    猜你喜欢
    • 2016-03-16
    • 2016-11-07
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-22
    • 2013-12-01
    相关资源
    最近更新 更多