Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 864
scrit Avatar asked Sep 09 '14 17:09

scrit


2 Answers

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 { }
        }
}
like image 158
James Avatar answered Oct 26 '22 15:10

James


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

Following settings works for 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".

like image 2
Utkarsh Avatar answered Oct 26 '22 17:10

Utkarsh