本文主要介绍.NET(C#)中,使用CefSharp来实现模拟选择图片或文件,以及相关的示例代码。

1、实现 IDialogHandler接口

使用CefSharp 实现模拟上传选择文件,需要实现IDialogHandler接口中 OnFileDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFileDialogMode mode, CefFileDialogFlags flags, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)方法。

具体如下:

using CefSharp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WebBrowser.OperateTask.Utils;
namespace CJavapy
{
    public class UploadFileDialogHandler : IDialogHandler
    {

        public bool OnFileDialog(IWebBrowser chromiumWebBrowser, IBrowser browser, CefFileDialogMode mode, CefFileDialogFlags flags, string title, string defaultFilePath, List<string> acceptFilters, int selectedAcceptFilter, IFileDialogCallback callback)
        {
            callback.Continue(selectedAcceptFilter, new List<string> { @"E:\softwaredir\img\4.mp4" });
            return true;
        }
    }
}

2、配置使用UploadFileDialogHandler处理文件上传

ChromiumWebBrowser对象的DialogHandler属性,需要配置成UploadFileDialogHandler,在CefSharp中,点击上传按钮时,就会触发执行上面实现的OnFileDialog()方法,返回的值就是要上传文件的路径,可以是多个路径,此时就会弹出打开选择文件的对话窗口,而是直接后台返回。

配置代码如下:

 CefSettings settings = new CefSettings();
            settings.CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), ConfigurationManager.AppSettings["CookiePath"], "CEF");
            settings.IgnoreCertificateErrors = true;
            settings.WindowlessRenderingEnabled = true;
            settings.SetOffScreenRenderingBestPerformanceArgs();
            settings.UserAgent = string.IsNullOrEmpty(System.Configuration.ConfigurationManager.AppSettings["UserAgent"]) ? "Mozilla/5.0 (iPhone; CPU iPhone OS 10_0_1 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/14A403 Safari/602.1" : ConfigurationManager.AppSettings["UserAgent"];
            //settings.CefCommandLineArgs.Add("-touch-devices", "enabled");
            //settings.CefCommandLineArgs.Add("-touch-events", "enabled");
            //settings.CefCommandLineArgs.Add("-enable-pinch", "enabled");
            //settings.CefCommandLineArgs.Add("-touchview", "enabled");
            //settings.CefCommandLineArgs.Add("-touch-view", "enabled");
            //settings.CefCommandLineArgs.Add("-material-hybrid", "enabled");
            //settings.CefCommandLineArgs.Add("touch-devices", "enabled");
            //settings.CefCommandLineArgs.Add("touch-events", "enabled");
            //settings.CefCommandLineArgs.Add("enable-pinch", "enabled");
            //settings.CefCommandLineArgs.Add("touchview", "enabled");
            //settings.CefCommandLineArgs.Add("touch-view", "enabled");
            //settings.CefCommandLineArgs.Add("material-hybrid", "enabled");
            Cef.Initialize(settings);
ChromiumWebBrowser  browser = new ChromiumWebBrowser(ConfigurationManager.AppSettings["LoginUrl"]);
            browser.DialogHandler = new UploadFileDialogHandler();

推荐文档