Javascript 从链接打开本地文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5246292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open local folder from link
提问by Satya
How can I open a local folder view by clicking on any link?
如何通过单击任何链接打开本地文件夹视图?
I tried many options like
我尝试了很多选择,比如
<a href="file:///D:/Tools/">Open folder</a>
or
<a href="file:///D:/Tools/">Open folder</a>
或者
<a onclick="file:///D:/Tools/">Open folder</a>
or
<a onclick="file:///D:/Tools/">Open folder</a>
或者
<a onclick="window.open(file:///D:/Tools/)">Open folder</a>
<a onclick="window.open(file:///D:/Tools/)">Open folder</a>
采纳答案by Pekka
Linking to local resources is disabled in all modern browsers due to security restrictions.
由于安全限制,所有现代浏览器都禁止链接到本地资源。
For Firefox:
对于火狐:
For security purposes, Mozilla applications block links to local files (and directories) from remote files. This includes linking to files on your hard drive, on mapped network drives, and accessible via Uniform Naming Convention (UNC) paths. This prevents a number of unpleasant possibilities, including:
- Allowing sites to detect your operating system by checking default installation paths
- Allowing sites to exploit system vulnerabilities (e.g., C:\con\con in Windows 95/98)
- Allowing sites to detect browser preferences or read sensitive data
出于安全目的,Mozilla 应用程序会阻止从远程文件到本地文件(和目录)的链接。这包括链接到硬盘驱动器、映射网络驱动器上的文件,以及通过统一命名约定 (UNC) 路径访问的文件。这可以防止许多不愉快的可能性,包括:
- 允许站点通过检查默认安装路径来检测您的操作系统
- 允许站点利用系统漏洞(例如,Windows 95/98 中的 C:\con\con)
- 允许站点检测浏览器首选项或读取敏感数据
for IE:
对于IE:
Internet Explorer 6 Service Pack 1 (SP1) no longer allows browsing a local machine from the Internet zone. For instance, if an Internet site contains a link to a local file, Internet Explorer 6 SP1 displays a blank page when a user clicks on the link. Previous versions of Windows Internet Explorer followed the link to the local file.
Internet Explorer 6 Service Pack 1 (SP1) 不再允许从 Internet 区域浏览本地计算机。例如,如果 Internet 站点包含指向本地文件的链接,则当用户单击该链接时,Internet Explorer 6 SP1 会显示一个空白页面。以前版本的 Windows Internet Explorer 遵循本地文件的链接。
for Opera(in the context of a security advisory, I'm sure there is a more canonical link for this):
对于Opera(在安全公告的上下文中,我确定有一个更规范的链接):
As a security precaution, Opera does not allow Web pages to link to files on the user's local disk
作为安全预防措施,Opera 不允许网页链接到用户本地磁盘上的文件
回答by JFish222
Solution: Launching a Downloadable Link
解决方案:启动可下载链接
The following works in all browsers, but as always there are caveats.
以下适用于所有浏览器,但一如既往地有警告。
Background:
背景:
"URL Shortcuts" are OS dependent. The following solution is for MS Windows due to a lack of standards between environments.
“URL 快捷方式”取决于操作系统。由于环境之间缺乏标准,以下解决方案适用于 MS Windows。
If you require linux support for the solution below, please see this article.
* I have no connection to the article, YMMV.
如果您需要以下解决方案的 linux 支持,请参阅此文章。
* 我与这篇文章没有任何关系,YMMV。
URL shortcuts come in two forms:
URL 快捷方式有两种形式:
Files with .URL extensions are text based. Can be dynamically generated.
[InternetShortcut]
URL=file:///D:/Files with .LNK extension are binary. They can be generated dynamically, but require iShelLinkInterface implementer. This is complicated by default OS restrictions, which rightfully prevent an IIS process from reaching Shell.
带有 .URL 扩展名的文件是基于文本的。可以动态生成。
[互联网快捷方式]
URL=file:///D:/带有 .LNK 扩展名的文件是二进制文件。它们可以动态生成,但需要 iShelLinkInterface 实现者。默认操作系统限制使这变得复杂,这理所当然地阻止了 IIS 进程访问 Shell。
.URL is the recommended solution, as dynamic generation is viable across Web Languages/Frameworks and allows for a KISS implementation.
.URL 是推荐的解决方案,因为动态生成跨 Web 语言/框架是可行的,并允许 KISS 实现。
Overview/Recap:
概述/回顾:
- Security restrictions will not allow you to open a path/launch explorer directly from the page (as stated by @Pekka).
- Sites hosted externally (not on your local computer) will not allow file:///... uri's under default security permissions.
- 安全限制不允许您直接从页面打开路径/启动资源管理器(如@Pekka 所述)。
- 外部托管的站点(不在您的本地计算机上)将不允许 file:///... uri 在默认安全权限下。
Solution:
Provide a downloadable link (.URL or .LNK) to the resource. Browser behavior will be explained at end of post.
Option 1:Produce a .lnk file and save it to the server. Due to the binary nature of the .LNK file, this is not the recommended solution, but a pre-generated file should be viable.
Option 2:Produce a .url file and either save it to the server or dynamically generate
it. In my situation, I am dynamically creating the .URL file.
解决方案:
提供资源的可下载链接(.URL 或 .LNK)。浏览器行为将在帖子末尾解释。
选项 1:生成 .lnk 文件并将其保存到服务器。由于 .LNK 文件的二进制性质,这不是推荐的解决方案,但预先生成的文件应该是可行的。
选项 2:生成 .url 文件并将其保存到服务器或动态生成它。在我的情况下,我正在动态创建 .URL 文件。
Solution Details (.URL):
解决方案详细信息 (.URL):
Add .url to the available MIME types in your web server.
For IIS open the site, choose MIME Types, and add the following:File name Extension= .url
MIME type: application/internet-shortcutPer @cremax ... For Webkit Browsers like Chrome on Apache Servers add this code to .htaccess or http.config:
SetEnvIf Request_URI ".url$" requested_url=url Header add Content-Disposition "attachment" env=requested_url
The .url file is a text file formatted as follows (again, this can be dynamically generated).
File Contents:
[InternetShortcut]
URL=file:///D:Provide a link to the script that generates the .url file, or to the file itself.
If you've simply uploaded a .url file to your server, add the following to your HTML:
<a href="URIShortcut.url">Round-About Linking</a>
将 .url 添加到 Web 服务器中可用的 MIME 类型。
对于 IIS 打开站点,选择 MIME 类型,并添加以下内容:文件名扩展名= .url
MIME 类型:application/internet-shortcut每 @cremax ... 对于 Apache 服务器上的 Chrome 等 Webkit 浏览器,将此代码添加到 .htaccess 或 http.config:
SetEnvIf Request_URI ".url$"requested_url=url 头部添加 Content-Disposition "attachment" env=requested_url
.url 文件是一个文本文件,格式如下(同样,这可以动态生成)。
文件内容:
[互联网快捷方式]
URL=file:///D:提供指向生成 .url 文件的脚本或文件本身的链接。
如果您只是将 .url 文件上传到您的服务器,请将以下内容添加到您的 HTML 中:
<a href="URIShortcut.url">Round-About Linking</a>
Browser Dependent Behavior
浏览器相关行为
Chrome: Download/Save file.url then open
In Chrome, this behavior can be augmented by choosing the "Always open files of this type" option.
Chrome:下载/保存 file.url 然后打开
在 Chrome 中,可以通过选择“始终打开此类型的文件”选项来增强此行为。
FireFox: Download/Save file.url then open
FireFox:下载/保存 file.url 然后打开
Internet Explorer: Click “Open” and go straight to directory (no need to save shortcut)
Internet Explorer: 点击“打开”并直接进入目录(无需保存快捷方式)
Internet Explorer has the preferred behavior, but Chrome and Firefox are at least serviceable.
Internet Explorer 具有首选行为,但 Chrome 和 Firefox 至少可以使用。
回答by Leon
Only IE6-8 - there's an ActiveX workaround this local-files issue in JavaScript:
仅适用于 IE6-8 - JavaScript 中有一个 ActiveX 解决此本地文件问题:
function OpenImage(filePath)
{
var myshell = new ActiveXObject("WScript.shell");
myshell.run(filePath, 1, true);
}
回答by felixsigl
you can use
您可以使用
<a href="\computername\folder">Open folder</a>
in Internet Explorer
在 Internet Explorer 中
回答by cremax
add on click open local directory o local file to google chrome:
添加点击打开本地目录o本地文件到谷歌浏览器:
The solution from JFish222 works ( URL file solution )
JFish222 的解决方案有效(URL 文件解决方案)
For Webkid Browsers like Chrome on Apache Servers just add to .htaccess o http.config this code:
对于 Apache 服务器上的 Chrome 等 Webkid 浏览器,只需将以下代码添加到 .htaccess o http.config 中:
SetEnvIf Request_URI ".url$" requested_url=url Header add Content-Disposition "attachment" env=requested_url
SetEnvIf Request_URI ".url$"requested_url=url 头部添加 Content-Disposition "attachment" env=requested_url
And by the first downlod of your url file click on the file in chromes downloadbar and select "always open this file".
并通过您的 url 文件的第一个下载单击 chromes 下载栏中的文件并选择“始终打开此文件”。
回答by W.CURRY
Local Explorer- File Manager on web browser extention can solve for chrome
本地资源管理器- Web 浏览器扩展上的文件管理器可以解决 chrome
but still some encoding problems
但仍然存在一些编码问题