如何在没有http服务器的情况下使用file://网址从Chrome使用File System Access API [英] How do I use the File System Access API from Chrome using file:// url with no http server
问题描述
我想在没有http服务器的情况下从计算机上的Chrome打开html文件,并且希望HTML文件中的JavaScript能够读取和写入本地文件系统中的文件,以及浏览目录. /p>
如何使用文件系统API执行此操作: https://wicg.github .io/file-system-access/吗?
Chrome 85当前不提供文件系统API.现在,您可以使用批处理文件启动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 chooseFileSystemEntries === 'undefined')
访问目录或文件
const directoryHandle = await chooseFileSystemEntries({type: 'open-directory'})
const fileHandle = await chooseFileSystemEntries({type: 'open-file'})
使用text()
api等调用getFile()
获取要处理的文件后,
参见 https://wicg.github.io/file-system-access/更多
I want to open an html file from Chrome on my computer, without an http server, and I want the JavaScript in the HTML file to be able read and write files in the local file system, and browse directories as well.
How do I do this using the File System API: https://wicg.github.io/file-system-access/ ?
File System API is not available currently in Chrome 85. For now you can launch the html file using a batch file which will locate and launch Chrome with the right appropriate command line options.
Name the batch file the same name as the html file, and place the following in the batch file:
@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%"
In the javascript you can make calls like this:
Determine if API is available
if (typeof chooseFileSystemEntries === 'undefined')
Access directory or file
const directoryHandle = await chooseFileSystemEntries({type: 'open-directory'})
const fileHandle = await chooseFileSystemEntries({type: 'open-file'})
After calling getFile()
to get the file to be worked on, using text()
api, etc.
See https://wicg.github.io/file-system-access/ for more
这篇关于如何在没有http服务器的情况下使用file://网址从Chrome使用File System Access API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!