Andrew
Andrew

Reputation: 11479

Cef - upload file without shown OpenFileDialog

I need to have a way to select some file without display of OpenFileDialog.

Yes, I know that CEF is not the best way to automate sth, but I need to do this with CEF.

I have found that this is possible up from 2014: https://github.com/cefsharp/CefSharp/pull/342/commits/c11fe8e4e97179ff4073208c13f9ff29e61bab79

in this commit added ability to overriding file browse dialog result... But I still do not understand how to use this ability...

And I have found sample of usage, but it doesn't work:

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

it'ts shows me error that at the moment IDialogHandler in OnFileDialog have another parameters (without result).

Current parameters list is:

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

Can someone help me?

I'm using the latest CEFsharp: 63.0.3

Upvotes: 2

Views: 2566

Answers (1)

Andrew
Andrew

Reputation: 11479

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

And usage:

Browser.DialogHandler = new TempFileDialogHandler(files);

Upvotes: 3

Related Questions