How do I access local (file://) resources on a remote webpage?

3,333 views
Skip to first unread message

Ronny Chan

unread,
Jan 25, 2014, 8:24:53 PM1/25/14
to cefs...@googlegroups.com

I can't get CefSharp to load from a `file://` URL. to work from a remote webpage. This is what I have right now.

    BrowserSettings browserSettings = new BrowserSettings();
    browserSettings.FileAccessFromFileUrlsAllowed = true;
    browserSettings.UniversalAccessFromFileUrlsAllowed = true;
    browserSettings.DeveloperToolsDisabled = false;
            

    this.webview = new WebView("http://google.com", browserSettings);
            
As you can see, I have `FileAccessFromFileUrlsAllowed` and `UniversalAccessFromFileUrlsAllowed` set to true.

I'm trying to add a local image to the DOM with Javascript:

    this.webview.ExecuteScript(@"
        var img = document.createElement('img');
        img.setAttribute('src','file:///C:/test.jpg');
        document.getElementsByTagName('body')[0].appendChild(img);
    ");

It doesn't work; when I open up the developer console, this error pops up

`Not allowed to load local resource: file:///C:/test.jpg `

I've been trying to figure this out all day, thanks in advance.

 

Per Lundberg

unread,
Jan 28, 2014, 5:56:38 AM1/28/14
to cefs...@googlegroups.com
Hi Ronny,

I'm not sure this can be done. The settings you are referring to seems to work the other way around - they control whethere file://-based URL:s are allowed to in turn load other file:// resources, and also net resources (the UniversalAccessFromFileUrlsAllowed).

I looked briefly and I'm not sure there is such a setting that you are looking for. It would be quite a security risk, allowing any web page to allow any file on your local computer... :) But you can definitely do it if you like, you just have to implement a custom scheme (like myfile:// or whatever) where you have full control over what is allowed and what not. Take a look at the CefSharpSchemeHandler (in CefSharp3) for an example of how it can be done.

Best regards,
Per
Reply all
Reply to author
Forward
0 new messages