Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file with CefSharp WinForms

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.

like image 796
crasholino Avatar asked Dec 15 '15 12:12

crasholino


4 Answers

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);
        }
    }
}
like image 67
crasholino Avatar answered Oct 25 '22 06:10

crasholino


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!

like image 20
lorinet3 Avatar answered Oct 25 '22 05:10

lorinet3


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

// ...
like image 27
Eric Twose Avatar answered Oct 25 '22 05:10

Eric Twose


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

like image 38
lava Avatar answered Oct 25 '22 06:10

lava