I am working on building a primitive and basic web browser on which my workplace would like to host some internal applications. I"m using cefSharp
in a WinForms application written in C#. I've succeeded in building the browser to navigate the application, but I'm having trouble with the download handler. I would like to download files directly to the C:\Users\[username]\Downloads
folder (all of our computers are Windows computers) without having to use the dialog.
Reading from Force CEFSharp to download without showing dialog suggests that using showDialog: false
should work, but when I apply this, nothing downloads. Likewise, I've made no progress by studying any of the following:
- WPF : download files through CefSharp
- https://github.com/cefsharp/CefSharp/blob/cd934267c65f494ceb9ee75995cd2a1ca0954543/CefSharp.Example/DownloadHandler.cs
- WPF : download files through CefSharp
- https://groups.google.com/forum/?nomobile=true#!topic/cefsharp/bS8PhHRlSAc
- https://groups.google.com/forum/#!topic/cefsharp/3cMUHSGxPDc
As a bonus, it'd be nice to have the option to open the file, such as in Google Chrome, but this isn't strictly necessary.
The code below runs smoothly and approximates what I am attempting. This example opens to a GitHub Gist. Clicking on the "Download Zip" button on the right opens the dialog to download and save the file.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
using System.IO;
namespace ShinyChrome
{
public partial class ShinyApp : Form
{
public class DownloadHandler : 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);
}
}
}
public ShinyApp()
{
InitializeComponent();
}
ChromiumWebBrowser chrome;
private void ShinyApp_Load(object sender, EventArgs e)
{
CefSettings settings = new CefSettings();
Cef.Initialize(settings);
chrome = new ChromiumWebBrowser("https://gist.github.com/nutterb/32992747c1a69aa7a8fdcc2b5347178f");
chrome.DownloadHandler = new DownloadHandler();
this.shinyContainer.Controls.Add(chrome);
}
}
}
downloadItem.SuggestedFileName
is just a string that represents a path, can you not just replace it withC:\Users\[username]\Downloads
and then setshowDialog: false
? So, in your case, it would becallback.Continue("C:\Users\[username]\Downloads", showDialog: false);
. – Chenopodcallback.Continue($@"C:\Users\[username]\Downloads\{downloadItem.SuggestedFileName}", showDialog: false);
Note, though, you will need to implement some logic to prevent file name clashes. – Chenopod