简体   繁体   English

不允许加载本地资源:文件

[英]Not allowed to load local resource: file

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\4.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:/// C:/160.53.112.171myTestcons%04.1displayData.htm。

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 { }
        }
}

When you try to open a file with FILE:\\\\\\\\ via javascript in IE. 当您尝试通过IE中的javascript使用FILE:\\\\\\\\打开文件时。 IE will not allow you to open it (which is a by default behaviour) because of some security restrictions. 由于某些安全限制,IE不允许您打开它(默认情况下是一种行为)。 I have faced this IE security issue before in multiple projects. 我曾在多个项目中遇到过此IE安全问题。 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). 您可以尝试如下所述更改IE安全设置,但不建议这样做(因为您不能更改每个用户的设置来更改行为)。

Following settings works for IE 8 以下设置适用于IE 8

In Internet Explorer, go to Tools → Internet Options → Advanced. 在Internet Explorer中,转到工具→Internet选项→高级。 Scroll down to the Security section and check the box for "Allow active content to run on files on My Computer". 向下滚动到“安全性”部分,然后选中“允许活动内容在我的电脑上的文件上运行”框。

I have tried this in IE, Chrome and Firefox. 我已经在IE,Chrome和Firefox中进行了尝试。 It's working. 工作正常

<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. 请更新您的浏览器版本,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM