Search code examples
c#wpfcefsharpchromium-embedded

how to set --allow-file-access-from-files to cefsharp wpf?


i would test Cefsharp, but i dont see where to set --allow-file-access-from-files

i have tried

<Window x:Class="Chromium.MainWindow"
        xmlns:cef="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Chromium"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <cef:ChromiumWebBrowser x:Name="browser" IsBrowserInitializedChanged="browser_IsBrowserInitializedChanged"></cef:ChromiumWebBrowser>
    </Grid>
</Window>

    private void browser_IsBrowserInitializedChanged(object sender, DependencyPropertyChangedEventArgs e)
    {        
        browser.LoadUrl("file:///C:/index.html");
    }

but its no good.. is there a solution to accept local file?

inside the local file index.html, i am loading a local json file (json in same folder than index.html) with d3.son() (library d3.js)

<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
      :
      :
    var link = svg.append("g").selectAll(".link"),
        node = svg.append("g").selectAll(".node");

    d3.json("flare.json", function (error, classes) {
      :

The error displayed: "Access to XMLHttpRequest at 'file:///C:/flare.json' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, chrome, https, chrome-untrusted.", source: file:///C:/index.html (0)

i have no problem to call from Process.Start:

var p = System.Diagnostics.Process.Start("chrome.exe", "\"file:///C:/index.html\" --allow-file-access-from-files");

Solution

  • In fact its easy:

    just do that in the constructor:

                var settings = new CefSettings();
                settings.CefCommandLineArgs.Add("allow-file-access-from-files");
                settings.CefCommandLineArgs.Add("allow-universal-access-from-files");           
                Cef.Initialize(settings);