Home > Back-end >  How to implament DownloadHandler interface with CefSharp
How to implament DownloadHandler interface with CefSharp

Time:12-22

i wrote a Custom Download Handler Class in Visual studio and have completed the implamentation of the download handler but when i look at the errors it says that i need to implament a interface for MyCustomDownloadHandler and i dont know how but i have the actual interface code given to me in visual studio but i dont know what to put in the brackets

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

here is the MYCustomDownloadHandler Class Code:

    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 !");
            }
        }
    }
}

i couldent fit the namespace in the code, please forgive me i am farly new to formatting.

CodePudding user response:

Return true to proceed with the download or false to cancel the download.

If you wish to allow the download then return true;

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