21

I'm trying to download some file (image, audio file, or something else) from my app using CefSharp WinForms. I read many other posts, but nothing seems to work. Do you have any sample code that implements the downloader for CefSharp?

I tried downloading some files, nothing happens.

Pang
  • 8,605
  • 144
  • 77
  • 113
crasholino
  • 597
  • 1
  • 5
  • 13
  • What are you trying to do exactly? Show a save dialog? – amaitland Dec 15 '15 at 12:57
  • Share the code you've tried. Can you be more specific than "nothing happens"? – sab669 Dec 15 '15 at 16:08
  • I read something about this on other post and I tried all of them, but nothing. I tried in the specific to implement code like this: https://groups.google.com/forum/?nomobile=true#!topic/cefsharp/bS8PhHRlSAc I would like to know the approach to use in this case, because I didn't find anything about that. – crasholino Dec 16 '15 at 14:16
  • My answer is for them who have my same problem, but of course this solution may be used by any other project who don't have the DownloadHandler class. This class is essential for the correct working of the download manager. – crasholino Dec 17 '15 at 10:45

3 Answers3

20

After 2 days, finally I did it. For the people who have the same problem, here is the simple solution. If, you are using MinimalExample, you have to download Cefsharp example (cefsharp-master) unzip it and do this:

  1. Right click on your project -> Add exisisting item
  2. Navigate in cefsharp-master -> CefSharp.example -> Select DownloadHandler.cs
  3. Go in your BrowserForm.cs class and type this:

    browser.DownloadHandler = new DownloadHandler();

  4. Done!



DownloadHandler.cs

// Copyright © 2013 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;

namespace CefSharp.Example.Handlers
{
    public class DownloadHandler : IDownloadHandler
    {
        public event EventHandler<DownloadItem> OnBeforeDownloadFired;

        public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

        public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            OnBeforeDownloadFired?.Invoke(this, downloadItem);

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

        public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            OnDownloadUpdatedFired?.Invoke(this, downloadItem);
        }
    }
}
juicebyjustin
  • 284
  • 6
  • 16
crasholino
  • 597
  • 1
  • 5
  • 13
  • 1
    Where should I put browser.DownloadHandler = new DownloadHandler(); ? Inside initial method or browser open method? – Robbi Nespu Nov 03 '16 at 06:53
  • Why is `callback` being disposed on `OnBeforeDownload` but not in `OnDownloadUpdated`. Shouldn't it be disposed on both? – Juan Apr 27 '21 at 11:51
15

To solve this, just download class DownloadHandler.cs found here.

After that, import it into your Visual Studio project, and add this line to the code of the main form:

MyBrowser.DownloadHandler = new DownloadHandler();

and add this to the top of the code:

using CefSharp.Example;

Then try to download something from your browser, and it should work!

Pang
  • 8,605
  • 144
  • 77
  • 113
lorinet3
  • 182
  • 2
  • 10
10

I'm including the following because the implementation of OnBeforeDownloadFired() isn't shown in many online examples of how to use the DownloadHandler class, and it's missing from the DownloadHandler.cs cited.

This helped solve a nagging issue with downloading files (eg .mobi ebook) if the download link had the target "_blank". If there was no target, a download dialog was triggered. With a _blank target, I had to suppress a popup window and open a new custom tab in my browser, but when this happened, a download dialog was not triggered.

I think this is right. Hope it helps, or at least gives you a start:

DownloadHandler downer = new DownloadHandler(this);
browser.DownloadHandler = downer;
downer.OnBeforeDownloadFired += OnBeforeDownloadFired;
downer.OnDownloadUpdatedFired += OnDownloadUpdatedFired;

private void OnBeforeDownloadFired(object sender, DownloadItem e)
{
    this.UpdateDownloadAction("OnBeforeDownload", e);
}

private void OnDownloadUpdatedFired(object sender, DownloadItem e)
{
    this.UpdateDownloadAction("OnDownloadUpdated", e);
}

private void UpdateDownloadAction(string downloadAction, DownloadItem downloadItem)
{
    /*
    this.Dispatcher.Invoke(() =>
    {
        var viewModel = (BrowserTabViewModel)this.DataContext;
        viewModel.LastDownloadAction = downloadAction;
        viewModel.DownloadItem = downloadItem;
    });
    */
}

// ...

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

    public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

    MainForm mainForm;

    public DownloadHandler(MainForm form)
    {
        mainForm = form;
    }

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

// ...
Eric Twose
  • 163
  • 2
  • 8
  • This works great, all file download statuses are there, rest is easy to implement, thanks for this hint! – SturmCoder Oct 23 '17 at 13:17
  • I don't know how "Continue" parameters were when the "CefSharp.Example" was written. At this time, you can safely replace "showDialog: true" by "true". – Tiago Freitas Leal May 04 '18 at 15:29