|
using CefSharp;
using System.Windows.Forms;
…………………………………
this.WindowBrowser.DownloadHandler = new DownloadHandler(); // Assign the new instance of the Download Handler to web chromium browserinstance
………………………………………………..
public class DownloadHandler : IDownloadHandler
{
public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
if (!callback.IsDisposed)
{
using (callback)
{
SaveFileDialog saveFileDialog = new SaveFileDialog(); // using the win forms show dialog save the file
saveFileDialog.FileName = downloadItem.SuggestedFileName;
saveFileDialog.Filter = "|*" + System.IO.Path.GetExtension(saveFileDialog.FileName);
if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.FileName != "")
{
downloadItem.SuggestedFileName = saveFileDialog.FileName;
}
callback.Continue(downloadItem.SuggestedFileName, showDialog: false); // Callback until the download is completed
}
}
}
public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
if (downloadItem.IsComplete || downloadItem.IsCancelled) {
return; // once the download is completed stop the process
}
}
}
|
|
//Comment or remove the below lines at Program.cs as don’t require in CefSharp v63.0.3
if(!Cef.Initialize(settings))
{
if(Environment.GetCommandLineArgs().Contains("--type=renderer"))
Environment.Exit(0);
} |