0

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?
            }
pdf to image
  • 369
  • 6
  • 23
  • Try calling StartDownload instead of Load, search the source to find the method. – amaitland Feb 13 '17 at 03:22
  • where can i find that sir @amaitland? – pdf to image Feb 13 '17 at 03:52
  • GitHub is where you'll find the source – amaitland Feb 13 '17 at 04:03
  • this is what you meant sir? https://github.com/cefsharp/CefSharp/blob/84930b0784fb8d934af22f4f3cd8a089af6eccf1/CefSharp/IBrowserHost.cs but how can i use it? – pdf to image Feb 13 '17 at 04:05
  • @amaitland sir i updated the question above, i implemented the IBrowserHost interface what code do i need? i don't know what to search next. thank you so much – pdf to image Feb 13 '17 at 04:14
  • IBrowserHost is not an interface you implement yourself, it's exposed by the framework. Look at the WebBrowserExtensions.cs classe for examples – amaitland Feb 13 '17 at 04:37
  • @amaitland do you have a sample code sir? after 3 hrs of trying, i still can't download other files, – pdf to image Feb 13 '17 at 08:30
  • Nope, no example. I've never tried downloading multiple files, so it was only a suggestion. – amaitland Feb 13 '17 at 08:42
  • the only solution i have in my mind is check if the file is downloaded, then navigate to the next download link. i have another question sir. how can i make the savefile dialog hidden, and set the save location dynamically? thank you. so that i can make the program download without the save file dialog – pdf to image Feb 13 '17 at 08:47
  • 1
    Implement IDialogHandler – amaitland Feb 13 '17 at 09:10
  • @amaitland thank you, this is the code i used in order to download the file without save file dialog callback.Continue(System.IO.Path.GetTempPath() + downloadItem.SuggestedFileName, showDialog: false); – pdf to image Feb 14 '17 at 06:59

2 Answers2

1

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

// ...
Eric Twose
  • 173
  • 2
  • 9
1

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);
}
jfvoviedo
  • 11
  • 1