【问题标题】:Cef - upload file without shown OpenFileDialogCef - 上传文件而不显示 OpenFileDialog
【发布时间】:2019-01-12 09:07:15
【问题描述】:

我需要一种方法来选择一些文件而不显示 OpenFileDialog。

是的,我知道 CEF 不是自动化某事的最佳方式,但我需要使用 CEF 来做到这一点。

我发现从 2014 年开始,这是可能的: https://github.com/cefsharp/CefSharp/pull/342/commits/c11fe8e4e97179ff4073208c13f9ff29e61bab79

在此提交中添加了覆盖文件浏览对话框结果的功能...但我仍然不明白如何使用此功能...

我找到了用法示例,但它不起作用:

using System.Collections.Generic;
using System.IO;
namespace CefSharp.Example
{
    public class TempFileDialogHandler : IDialogHandler
    {
        public bool OnFileDialog(IWebBrowser browser, string title, string defaultFileName, List<string> acceptTypes, out List<string> result)
        {
            result = new List<string> { Path.GetRandomFileName() };
            return true;
        }
    }
}

它向我显示错误,目前 OnFileDialog 中的 IDialogHandler 有另一个参数(没有结果)。

当前参数列表为:

public bool OnFileDialog(IWebBrowser browserControl, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)

有人可以帮我吗?

我正在使用最新的 CEFsharp:63.0.3

【问题讨论】:

  • “它向我显示错误” - 我不清楚接下来的哪些部分是错误消息。
  • ofc 它告诉我没有具有此类参数的方法。我已经在上面的文本中写了当前参数列表:) OnFileDialog 参数之间的区别你可以在两个代码示例中看到......所以错误文本就像TempFileDialogHandler do not implement IDialogHandler

标签: c# chromium openfiledialog chromium-embedded cefsharp


【解决方案1】:
public class TempFileDialogHandler : IDialogHandler
{
    string[] _filePath;

    public TempFileDialogHandler(params string[] filePath)
    {
        _filePath = filePath;
    }

    public bool OnFileDialog(IWebBrowser browserControl, IBrowser browser, CefFileDialogMode mode, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)
    {
        callback.Continue(0, _filePath.ToList());
        return true;
    }
}

及用法:

Browser.DialogHandler = new TempFileDialogHandler(files);

【讨论】:

    猜你喜欢
    • 2019-09-27
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 2015-08-20
    • 2020-08-19
    相关资源
    最近更新 更多