如何在“—— allow-file-access-from-files”模式下使用 Chrome 启动 html?

我对 给你也有同样的情况

为了解决这个问题,我必须在“—— allow-file-access-from-files”模式下使用 Chrome 启动 html 文件。 我尝试了很多次后续步骤,但都没有用。

  1. 在 Windows7下启动 cmd
  2. 直接转到 chrome.exe 文件夹
  3. 做这个 chrome --allow-file-access-from-files file:///C:/test%20-%203.html
434231 次浏览

搜索 Chrome 可执行文件的路径,然后在 cmd 上尝试:

> "C:\PathTo\Chrome.exe" --allow-file-access-from-files

来源

编辑: 正如我在你的问题中看到的,不要忘记 Windows 和 Unix 有点类似,所以当你输入“ Chrome...”时,cmd 会在 PATH 中搜索 Chrome,但是一般来说,Chrome 文件夹不在 PATH 中。另外,你没有为你的可执行文件指定扩展名... 所以如果你移动到 Chrome 的文件夹,这个命令可能也会起作用:

> .\chrome.exe --allow-file-access-from-files

不要这样做! 你会让你的机器受到攻击。而是运行一个本地服务器

cd path/to/files
python -m SimpleHTTPServer

然后将浏览器指向

http://localhost:8000

如果你发现它太慢 考虑一下这个解决方案

在撰写本文时,在 OSX 中,它通常是这样的

"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --allow-file-access-from-files

如果你像我一样是一个怪胎,并把你的应用程序在 ~/Applications,那么它将是

"/Users/yougohere/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --allow-file-access-from-files

如果这两个都不起作用,那么在 Chrome 地址栏中键入 chrome://version,它会告诉你应该使用什么样的“命令行”调用。加上 --allow-file-access-from-files

那面旗子很危险!_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _.默认情况下,来自任何地方、本地或 Web 的文档都不应该访问本地文件:///resources。

更好的解决方案是在本地运行一个小的 http 服务器。

- 对于 Windows 来说

最简单的方法是使用节点的包管理器在全球范围内安装 http-server:

npm install -g http-server

然后只需在任何项目目录中运行 http-server:

例如 d:\my_project> http-server

Starting up http-server, serving ./
Available on:
http:169.254.116.232:8080
http:192.168.88.1:8080
http:192.168.0.7:8080
http:127.0.0.1:8080
Hit CTRL-C to stop the server

或者像 prusswan 建议的那样,您也可以在 windows 下安装 Python,并遵循下面的说明。

为了 Linux

由于 Python 通常在大多数 Linux 发行版中都可用,所以只需在项目目录中运行 python -m SimpleHTTPServer,就可以在 http://localhost:8000上加载页面

在 Python3中,SimpleHTTPServer模块已经合并到 http.server中,因此新的命令是 python3 -m http.server

简单,并且没有意外使您的浏览器打开易受攻击的安全风险。

如果你正在使用 Mac,你可以使用以下终端命令:

open -a Google\ Chrome --args --allow-file-access-from-files

退出(强制退出)所有的 chrome 实例。否则下面的命令将不起作用。

open -a "Google Chrome" --args --allow-file-access-from-files

在终端中执行这个命令将打开 Chrome,而不管它安装在哪里。

您可能想尝试 Chrome 网络服务器,它使用 HTTP 从本地文件夹提供网页服务。它使用起来很简单,并且可以避免标记,正如前面提到的,这可能会使您的文件系统容易受到攻击。

Screenshot of Web Server for Chrome

在窗户上:

chrome --allow-file-access-from-files file:///C:/test%20-%203.html

在 linux 上:

google-chrome --allow-file-access-from-files file:///C:/test%20-%203.html
REM Kill all existing instance of chrome
taskkill /F /IM chrome.exe /T
REM directory path where chrome.exe is located
set chromeLocation="C:\Program Files (x86)\Google\Chrome\Application"
cd %chromeLocation%
cd c:
start chrome.exe --allow-file-access-from-files

将上面几行保存为 。蝙蝠文件

根据将被放入文件系统的文件,只要该文件不是恶意软件,那么这将是安全的。

但是不要担心向 File System 目录写/读文件,因为您可以通过给予适当的访问权限和安全限制来加强该目录的安全性(包括它的继承性)。读/写/修改。

默认情况下,文件系统、本地存储和存储目录位于“用户[当前用户] AppData 本地 GoogleChrome 用户数据默认”目录中。

然而,您可以使用“—— user-data-dir”标志对其进行定制。

这是一个例子:

"C:\Program Files (x86)\Google\Application\chrome.exe" --user-data-dir="C:\Chrome_Data\OO7" --allow-file-access-from-files

希望这对大家有帮助。

很快就可以运行一个需要许可或被 CORS 阻止的 html 只需使用 VSCODE 打开文件夹并安装一个名为“ live server”的扩展即可

然后点击底部,上面写着直播,就是这样。 截图

我尝试使用允许文件访问,但认为有一个更好的方法。找到了通过 http 传送本地文件共享文件的代码。

Https://gist.github.com/andrii-riabchun/07f854939ce776f6e54ba6b64f43cc92

我添加了一些编辑建议。我使用了修订代码和编辑建议。还添加了一个简单的脚本来创建在 webserver 上运行的 ls 的 index.html。

我吸毒

google-chrome-beta --user-data-dir="/tmp/tmp-chrome-user-data-dir" --allow-file-access-from-files --disable-web-security file:///home/myuser/projects/myproject/myhtml.html

当我尝试 request('file://xxx')时禁用 has been blocked by CORS policy