如何使用cefsharp下载多个文件。
我可以使用此代码下载文件。但我的问题是它只下载第一个链接。如何让cefsharp下载多个文件。
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);
}
}
}
在我的项目中,我
实现接口
class DownloadMe : IBrowserHost{
}
public void StartDownload(string url)
{
//what code do i need here?
}
答案 0 :(得分:1)
我包含以下内容,因为OnBeforeDownloadFired()的实现未在许多有关如何使用DownloadHandler类的在线示例中显示。
如果下载链接具有目标“_blank”,这有助于解决下载文件(例如.mobi电子书)的唠叨问题。如果没有目标,则会触发下载对话框。使用_blank目标,我不得不在浏览器中禁止弹出窗口并打开一个新的自定义选项卡,但是当发生这种情况时,不会触发下载对话框。
我认为这是对的。希望它有所帮助,或者至少给你一个开始:
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;
}
// ...
答案 1 :(得分:0)
在您的代码中:
foreach (var item in ListofLinks)
{
browser.Load(item);
}
将其更改为:
foreach (var item in ListofLinks){
var cefBrowser = browser.GetBrowser();
IBrowserHost ibwhost = cefBrowser == null ? null : cefBrowser.GetHost();
ibwhost.StartDownload(item);
}