How do I use the File System Access API from Chrome using file:// url with no http server

守給你的承諾、 提交于 2020-12-15 07:09:44

问题


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/ ?


回答1:


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



来源:https://stackoverflow.com/questions/64129713/how-do-i-use-the-file-system-access-api-from-chrome-using-file-url-with-no-ht

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!