访问本地文件仅适用于本地 HTML 文件

Access to local files only work with local HTML file

我想通过单击 HTML link

来访问计算机上的本地文件
<a href="file:///\mynetworklink\users\myusername\public\mydoc.docx">Link to local file</a>

如果我从本地 HTML 文件中执行它,效果很好,例如 :

file:///D:/Profiles/myusername/Desktop/testfile.html

它甚至可以通过使用 Javascript 中的 window.open 运行良好:

    <script type="text/javascript">
    window.open('\\mynetworklink\users\myusername\public\mydoc.docx', 'MyWindow');
    </script>

但是当我使用 Apache 从我的本地 PHP 应用程序执行此操作时,如果不允许这样做。 尝试使用 link 标签时的错误示例:

Not allowed to load local resource

所以我听说 Chrome/Firefox/IE 出于安全原因阻止以这种方式访问​​本地文件。

有没有什么办法可以在不修改浏览器设置的情况下让它工作? 这个应用程序被很多客户使用,我不能要求大家修改那里的设置。

谢谢, 最好的问候。

So I heard that Chrome/Firefox/IE are blocking local file access this way for security reasons.

Isn't there any way to make it work without modifying the browsers settings ?

没有。那将使它成为毫无意义的安全功能。

听起来您必须配置 Apache 才能在文件系统上映射资源:

看看here and probably use the Alias directive.

httpd.conf

Alias "/resources/" "C:/users/myusername/public/resources/"

然后如下使用:

<a href="/resources/mydoc.docx">Link to local file</a>

我找到了一个适用于 Firefox 的解决方案,带有一个插件 here

但您还需要下载一些东西,以便在安装插件后安装在 Windows 或 Linux 上。

我仍在为 IE 寻找相同的解决方案。

将您的应用分发为 ElectronJS 应用程序。这将使您能够无缝访问本地文件系统和网络功能。