0

I need to call event when I click on downloading button on the site and get downloading link immediately. I know how it works with WebBrowser:

private void WebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
    string url = e.Uri.ToString();
    if (url.Contains("https://accounts.google.com/AccountChooser") 
    {
        _userAgentController.UserAgentRefresh();
    }

    if (url.Contains("get:")) //specific form of download link
    {
        DownloadModel(url);
    }
}

but I should use it in CefSharp library and use DownloadModel() method that used download url.

I tried LoadingChanged and FrameLoadEnd events, but I can't get a needed link.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

1

Looks like CefSharp provides an interface for you to implement that has a callback for downloading.

Here's an example:

  public class DownloadHandler : IDownloadHandler
    {
        public event EventHandler<DownloadItem> OnBeforeDownloadFired;

        public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

        public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            var handler = OnBeforeDownloadFired;
            if (handler != null)
            {
                handler(this, downloadItem);
            }

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

        public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            var handler = OnDownloadUpdatedFired;
            if (handler != null)
            {
                handler(this, downloadItem);
            }
        }
    }

From here, once the download completes, you'll have the DownloadItem object, which provides the full-path that you need:

See class structure here: https://github.com/cefsharp/CefSharp/blob/0a2693fa9ba7273ada5df363bf78e85b5a1a342b/CefSharp/DownloadItem.cs

d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • Thanks, I used DownloadHandler.cs and use just OnBeforeDownloadFired to get download link and then don't perform downloading, so I can call my method – Nastya Kononchuk Oct 11 '17 at 14:14