如何在 CefSharp WPF 中设置 –allow-file-access-from-files?

huangapple go评论68阅读模式
英文:

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

问题

Here is the translated code portion you provided:

我想测试Cefsharp,但我不知道在哪里设置--allow-file-access-from-files。

我已经尝试过以下代码:

<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");
}

但这并不起作用... 是否有接受本地文件的解决方案?

在本地文件index.html中,我正在使用d3.son()(d3.js库)加载本地json文件(json文件与index.html位于同一文件夹中):

<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) {
      :

这是您提供的代码的翻译部分。

英文:

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

i have tried

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

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

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)

&lt;script src=&quot;https://d3js.org/d3.v4.min.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
      :
      :
    var link = svg.append(&quot;g&quot;).selectAll(&quot;.link&quot;),
        node = svg.append(&quot;g&quot;).selectAll(&quot;.node&quot;);

    d3.json(&quot;flare.json&quot;, 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(&quot;chrome.exe&quot;, &quot;\&quot;file:///C:/index.html\&quot; --allow-file-access-from-files&quot;);

答案1

得分: 0

在构造函数中执行以下操作:

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

In fact its easy:

just do that in the constructor:

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

huangapple
  • 本文由 发表于 2023年5月17日 20:28:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76272115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定