Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

javascript - 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.

Test<a href='#'onClick=window.open('file:\160.53.112.171myTestcons4.1displayData.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...