我在我的Windows项目中使用CefSharp [49] 下载处理程序用于根据特定条件取消下载。
下面是我的处理程序,我面临的问题是窗口打开是“打开文件”窗口而不是“另存为”窗口。
public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
if (!callback.IsDisposed)
{
using (callback)
{
if (_allowDownload)
callback.Continue(downloadItem.SuggestedFileName, showDialog: true);
else
downloadItem.IsCancelled = !_allowDownload;
}
}
}
答案 0 :(得分:0)
我使用以下代码作为解决方法
public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
if (!callback.IsDisposed)
{
using (callback)
{
if (_allowDownload)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.FileName = downloadItem.SuggestedFileName;
saveFileDialog.Filter ="|*"+ System.IO.Path.GetExtension(saveFileDialog.FileName);
if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.FileName != "")
{
downloadItem.SuggestedFileName = saveFileDialog.FileName;
}
else
**downloadItem.IsCancelled = _allowDownload;**
}
else
**downloadItem.IsCancelled = !_allowDownload;**
callback.Continue(downloadItem.SuggestedFileName, showDialog: false);
}
}
}
public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
if (downloadItem.IsComplete || downloadItem.IsCancelled)
browser.CloseBrowser(true);
}