winforms 如何使用CefSharp实现DownloadHandler接口

olmpazwi  于 2022-12-23  发布在  其他
关注(0)|答案(1)|浏览(413)

我在Visual Studio中编写了一个自定义下载处理程序类,并已完成下载处理程序的实现,但当我查看错误时,它说我需要实现MyCustomDownloadHandler的接口,我不知道如何实现,但我在Visual Studio中获得了实际的接口代码,但我不知道该在括号中放入什么

public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
    {
        //i dont know what to enter here.
    }

以下是MYCustomDownloadHandler类代码:

public event EventHandler<DownloadItem> OnBeforeDownloadFired;

    public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

    public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
    {
        //What do i enter here.
    }

    public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
    {
        if (downloadItem.IsValid)
        {
            Console.WriteLine("== File information ========================");
            Console.WriteLine(" File URL: {0}", downloadItem.Url);
            Console.WriteLine(" Suggested FileName: {0}", downloadItem.SuggestedFileName);
            Console.WriteLine(" MimeType: {0}", downloadItem.MimeType);
            Console.WriteLine(" Content Disposition: {0}", downloadItem.ContentDisposition);
            Console.WriteLine(" Total Size: {0}", downloadItem.TotalBytes);
            Console.WriteLine("============================================");
        }

        OnBeforeDownloadFired?.Invoke(this, downloadItem);

        if (!callback.IsDisposed)
        {
            using (callback)
            {
                callback.Continue(
                    downloadItem.SuggestedFileName,
                    showDialog: true
                );
            }
        }
    }

    /// https://cefsharp.github.io/api/51.0.0/html/T_CefSharp_DownloadItem.htm
    public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
    {
        OnDownloadUpdatedFired?.Invoke(this, downloadItem);

        if (downloadItem.IsValid)
        {
            // Show progress of the download
            if (downloadItem.IsInProgress && (downloadItem.PercentComplete != 0))
            {
                Console.WriteLine(
                    "Current Download Speed: {0} bytes ({1}%)",
                    downloadItem.CurrentSpeed,
                    downloadItem.PercentComplete
                );
            }

            if (downloadItem.IsComplete)
            {
                Console.WriteLine("The download has been finished !");
            }
        }
    }
}

我可以在代码中适应命名空间,请原谅我,我是一个很新的格式。

6jygbczu

6jygbczu1#

返回true继续下载,或返回false取消下载。
如果您希望允许下载,则返回true;

public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
{
    return true;
}

相关问题