pdf to image
pdf to image

Reputation: 369

Download multi files using cefsharp

How I download multiple files using cefsharp.

I can download file using this code. But my problem is it only download the first link. How can i make the cefsharp to download multiple files.

foreach (var item in ListofLinks)
                {
browser.Load(item);
}

//on my form load

browser.DownloadHandler = new MyDownloadHandler();

    class MyDownloadHandler : 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);
                    }

                }
            }

i added the https://github.com/cefsharp/CefSharp/blob/84930b0784fb8d934af22f4f3cd8a089af6eccf1/CefSharp/IBrowserHost.cs

in my project and i

implement interface

class DownloadMe : IBrowserHost{

}


public void StartDownload(string url)
            {
                //what code do i need here?
            }

Upvotes: 0

Views: 2850

Answers (2)

jfvoviedo
jfvoviedo

Reputation: 11

In your code:

foreach (var item in ListofLinks)
                {
browser.Load(item);
}

Change it for:

foreach (var item in ListofLinks){
var cefBrowser = browser.GetBrowser();
IBrowserHost ibwhost = cefBrowser == null ? null : cefBrowser.GetHost();
ibwhost.StartDownload(item);
}

Upvotes: 1

Eric Twose
Eric Twose

Reputation: 173

I'm including the following because the implementation of OnBeforeDownloadFired() isn't shown in many online examples of how to use the DownloadHandler class.

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

// ...

Upvotes: 1

Related Questions