如何将--allow-file-access-from-files设置为cefsharp wpf?

wwodge7n  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(188)

我会测试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) {
      :

错误显示:* “访问XMLHttpRequest at 'file:///C:/flare.json' from origin 'null' has been blocked by CORS policy:跨域请求仅支持协议方案:http,data,chrome-extension,chrome,https,chrome-untrusted.",来源:备案号:粤ICP备16036666号-1
我从进程调用没有问题。开始:

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

lsmepo6l1#

其实很简单:
在constructor中这样做:

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

相关问题