Download file with CefSharp WinForms
Asked Answered
C

4

25

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.

Camm answered 15/12, 2015 at 12:33 Comment(4)
What are you trying to do exactly? Show a save dialog?Aegeus
Share the code you've tried. Can you be more specific than "nothing happens"?Obadias
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: groups.google.com/forum/?nomobile=true#!topic/cefsharp/… I would like to know the approach to use in this case, because I didn't find anything about that.Camm
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.Camm
C
28

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);
        }
    }
}
Camm answered 16/12, 2015 at 17:48 Comment(4)
Where should I put browser.DownloadHandler = new DownloadHandler(); ? Inside initial method or browser open method?Cruller
Why is callback being disposed on OnBeforeDownload but not in OnDownloadUpdated. Shouldn't it be disposed on both?Thomey
thank you very much and a lot!!!! It takes me 3 days!Papilloma
Latest CEFSharp version requires the IDownloadHandler interface to implement the CanDownload function as wellLilalilac
L
19

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!

Lakeshialakey answered 12/12, 2016 at 18:14 Comment(2)
Link to source code returns Error 404. Better to include the source inline.Otherwise
works like a charm!Santiago
T
11

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

// ...
Testes answered 13/8, 2017 at 13:51 Comment(2)
This works great, all file download statuses are there, rest is easy to implement, thanks for this hint!Scan
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".Dirty
M
2

Add this in your MainForm

 CefBrowser = new ChromiumWebBrowser("http://google.com", null);
            CefBrowser.Margin = Padding.Empty;
            // browsers.Size = new Size(900, 600);// note here

            DownloadHandler downloadHandler = new DownloadHandler();
            CefBrowser.DownloadHandler = downloadHandler;

Just Create class DownloadHandler

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

finally it works like this enter image description here

Mariomariology answered 28/7, 2021 at 7:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.