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

时间:2014-09-09 17:39:31

标签: javascript html jsp google-chrome

我正在使用以下代码创建链接,当我点击IE浏览器中的链接时,我可以打开网址,但不能在Chrome中打开。

Test<a href='#'onClick=window.open('file:\\160.53.112.171\myTest\cons\4.1\displayData.htm','_self') >

以下是Chrome控制台上显示的错误消息。

  

不允许加载本地资源:文件:file:/// C:/160.53.112.171myTestcons%04.1displayData.htm in chrome。

3 个答案:

答案 0 :(得分:3)

对于任何登陆这里并在asp.net工作的人来说:

我在除IE之外的每个浏览器中都遇到了同样的问题(不允许加载本地资源)。

我的解决方法是将锚点直接指向处理程序&amp;包括路径的查询字符串:

<a href='handler.ashx?file=//server_name//dir//filename.pdf' />

然后处理程序将文件写为响应(根据需要使用_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 { }
        }
}

答案 1 :(得分:1)

当您尝试在IE中通过javascript使用FILE:\\\\打开文件时。由于某些安全限制,IE将不允许您打开它(这是默认行为)。我之前在多个项目中遇到过这个IE安全问题。您可以尝试更改以下给出的IE安全设置,但不建议这样做(因为您无法更改每个用户的设置以更改行为)。

以下设置适用于IE 8

  

在Internet Explorer中,转到工具→Internet选项→高级。向下滚动到“安全”部分,然后选中“#34;允许活动内容在我的计算机上的文件上运行&#34;”框。

答案 2 :(得分:-1)

我在IE,Chrome和Firefox中试过这个。它正在工作。

<a href='#'onClick=window.open('displayData.html','_self') >Test</a>

我不确定,但您的浏览器版本可能会影响它。请更新您的浏览器版本,然后重试。

相关问题