如何在没有http服务器的情况下使用file://网址从Chrome使用File System Access API

问题描述

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

如何使用文件系统API:https://wicg.github.io/file-system-access/

解决方法

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

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

replicacoes_sev

在javascript中,您可以这样拨打电话:

确定API是否可用

@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%"

访问目录或文件

if (typeof chooseFileSystemEntries === 'undefined')
const directoryHandle = await chooseFileSystemEntries({type: 'open-directory'})

使用const fileHandle = await chooseFileSystemEntries({type: 'open-file'}) api等调用getFile()以获取要处理的文件后, 有关更多信息,请参见https://wicg.github.io/file-system-access/

text()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...