This code is by ( https://ourcodeworld.com/articles/read/1446/how-to-allow-and-manipulate-downloads-in-cefsharp ) , The auto download path in the code refers to the directory where the downloaded files will be saved automatically without showing a file save dialog to the user. In the provided code, the auto download path is set to "C:\Users\sdkca\Downloads". ( Example path )
When the OnBeforeDownload method is triggered, it creates an IBeforeDownloadCallback object named callback, which is used to continue or cancel the download. The callback.Continue() method is called to specify the destination path for the downloaded file.
In the provided code, the destination path is constructed using the Path.Combine() method, which combines the auto download path (DownloadsDirectoryPath) and the suggested file name of the download item (downloadItem.SuggestedFileName). The DownloadsDirectoryPath variable contains the path to the directory where the file will be saved. In this example, the Path.Combine() method combines the DownloadsDirectoryPath and downloadItem.SuggestedFileName to create the full path for the downloaded file. The downloadItem.SuggestedFileName is the suggested name for the file provided by the server.
You can modify the DownloadsDirectoryPath variable to set the desired directory where the files should be automatically downloaded. Keep in mind that you should have appropriate permissions to write to that directory, and ensure the directory exists before running the code to avoid any exceptions.
With this implementation, when a download is triggered, the file will be automatically saved to the specified directory without any user intervention or file save dialog. The auto-download path is defined by the DownloadsDirectoryPath variable, and the file name is based on the downloadItem.SuggestedFileName.
using CefSharp;
using System;
using System.IO;
namespace CefsharpSandbox
{
class MyCustomDownloadHandler : IDownloadHandler
{
public event EventHandler<DownloadItem> OnBeforeDownloadFired;
public event EventHandler<DownloadItem> OnDownloadUpdatedFired;
public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
if (downloadItem.IsValid)
{
Console.WriteLine("== File information ========================");
Console.WriteLine(" File URL: {0}", downloadItem.Url);
Console.WriteLine(" Suggested FileName: {0}", downloadItem.SuggestedFileName);
Console.WriteLine(" MimeType: {0}", downloadItem.MimeType);
Console.WriteLine(" Content Disposition: {0}", downloadItem.ContentDisposition);
Console.WriteLine(" Total Size: {0}", downloadItem.TotalBytes);
Console.WriteLine("============================================");
}
OnBeforeDownloadFired?.Invoke(this, downloadItem);
if (!callback.IsDisposed)
{
using (callback)
{
// Define the Downloads Directory Path
// You can use a different one, in this example we will hard-code it
string DownloadsDirectoryPath = "C:\\Users\\sdkca\\Downloads\\";
callback.Continue(
Path.Combine(
DownloadsDirectoryPath,
downloadItem.SuggestedFileName
),
showDialog: false
);
}
}
}
/// https://cefsharp.github.io/api/51.0.0/html/T_CefSharp_DownloadItem.htm
public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
OnDownloadUpdatedFired?.Invoke(this, downloadItem);
if (downloadItem.IsValid)
{
// Show progress of the download
if (downloadItem.IsInProgress && (downloadItem.PercentComplete != 0))
{
Console.WriteLine(
"Current Download Speed: {0} bytes ({1}%)",
downloadItem.CurrentSpeed,
downloadItem.PercentComplete
);
}
if (downloadItem.IsComplete)
{
Console.WriteLine("The download has been finished !");
}
}
}
}
}