且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-11-17 09:01:45

在这里登陆并在asp.net工作的人:



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



我的解决方法是让锚点直接一个处理程序&包括路径的查询字符串:

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

然后处理程序会将该文件写为响应(按照需要在新选项卡中打开):

  public void ProcessRequest(HttpContext context){

if(context.Request [file ]!= null&&!String.IsNullOrEmpty(context.Request [file]。ToString()))
{
try
{
context.Response 。明确();
context.Response.ClearContent();
context.Response.ClearHeaders();
//您正在处理的任何内容类型
context.Response.ContentType =application / pdf;

//设置锚点的href时对路径进行编码,现在解码它
string file_name = HttpUtility.UrlDecode(context.Request [file]。ToString()) ;
context.Response.TransmitFile(file_name);


catch {}
}
}


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.

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.

Not allowed to load local resource: file:file:///C:/160.53.112.171myTestcons%04.1displayData.htm in chrome.

For anyone landing here and working in asp.net:

I was having the same issue (not allowed to load local resource) in every browser except 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):

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