Javascript 不允许加载本地资源:文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25750742/
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
Not allowed to load local resource: file
提问by scrit
I am using the code below to create a link, when I click on the link in IE it's working and I'm able to open the URL but not in Chrome.
我正在使用下面的代码创建一个链接,当我在 IE 中单击该链接时,它正在工作,我可以打开该 URL,但不能在 Chrome 中打开。
Test<a href='#'onClick=window.open('file:\160.53.112.171\myTest\cons.1\displayData.htm','_self') >
Below is the error message displayed on Chrome's console.
以下是 Chrome 控制台上显示的错误消息。
Not allowed to load local resource: file:file:///C:/160.53.112.171myTestcons%04.1displayData.htm in chrome.
不允许在 Chrome 中加载本地资源:file:file:///C:/160.53.112.171myTestcons%04.1displayData.htm。
回答by James
For anyone landing here and working in asp.net:
对于登陆这里并在 asp.net 工作的任何人:
I was having the same issue (not allowed to load local resource) in every browser except IE.
除了 IE,我在每个浏览器中都遇到了同样的问题(不允许加载本地资源)。
My workaround was to have the anchor direct to a handler & include a query string for the path:
我的解决方法是将锚点直接指向处理程序并包含路径的查询字符串:
<a href='handler.ashx?file=//server_name//dir//filename.pdf' />
Then the handler would write the file as the response (opens up in a new tab as desired with _self):
然后处理程序会将文件作为响应写入(根据需要使用 _self 在新选项卡中打开):
public void ProcessRequest (HttpContext context) {
if (context.Request["file"] != null && !String.IsNullOrEmpty(context.Request["file"].ToString()))
{
try
{
context.Response.Clear();
context.Response.ClearContent();
context.Response.ClearHeaders();
//whichever content type you're working with
context.Response.ContentType = "application/pdf";
//encode the path when you set the href of the anchor, so decode it now
string file_name = HttpUtility.UrlDecode(context.Request["file"].ToString());
context.Response.TransmitFile(file_name);
}
catch { }
}
}
回答by Utkarsh
When you try to open a file with FILE:\\\\via javascript in IE. IE will not allow you to open it (which is a by default behaviour) because of some security restrictions. I have faced this IE security issue before in multiple projects. You can try changing IE security settings as given below, but its not recommended (since you cannot change each and every User's setting to change the behaviour).
当您尝试FILE:\\\\在 IE 中通过 javascript打开文件时。由于某些安全限制,IE 不允许您打开它(这是默认行为)。我之前在多个项目中遇到过这个 IE 安全问题。您可以尝试更改 IE 安全设置,如下所示,但不建议这样做(因为您无法更改每个用户的设置来更改行为)。
Following settings works for IE 8
以下设置适用于 IE 8
In Internet Explorer, go to Tools → Internet Options → Advanced. Scroll down to the Security section and check the box for "Allow active content to run on files on My Computer".
在 Internet Explorer 中,转至工具 → Internet 选项 → 高级。向下滚动到“安全”部分并选中“允许活动内容在我的电脑上的文件上运行”框。
回答by Sanjay
I have tried this in IE, Chrome and Firefox. It's working.
我已经在 IE、Chrome 和 Firefox 中尝试过这个。它正在工作。
<a href='#'onClick=window.open('displayData.html','_self') >Test</a>
I am not sure, but it may be possible that your browser version affects it. Please update your browser version then try again.
我不确定,但您的浏览器版本可能会影响它。请更新您的浏览器版本,然后重试。

