I have built a Winforms Application using .NET 4.7.2 and CEFSharp 84.4.10.
I have a website loaded that has links to various files PDF, TXT, DOCX etc... The html for the links is like this:
<a href="relative_url/filename.ext" target="_blank">icon_here</a>
So when I click on a link to a docx for example, it CEFSharp opens a blank window over the top of my application and then opens the save file dialogue. When you press Save on the file dialogue it closes but the blank window stays open.
I want to do the following:
Not have the blank window open.
Detect the file type/mime being downloaded (my app only loads my site), and if they are PDF, TXT or DOCX then automatically download to a temp location, without showing the save dialogue, and open in the OS default viewer.
If not one of the above types then show the Save file dialogue and let user download to where they want.
I can work out how to open a file in the default OS viewer separately, I want to focus on automatic download of specific file types here.
I have spent hours searching for examples of how to do this and come up empty handed.
I was thinking I would be able to detect the type of file in DownloadHandler.OnBeforeDownload, and then based on the following post, just set showDialogue to false in the callback and then in DownloadHandler.OnDownloadUpdated detect downloadItem.IsComplete and then launch the file if it is of the correct type.
Force CEFSharp to download without showing dialog
However when I tried this I ran into the following issues:
The blank window still opens and stays open.
I find that if I set a break point in DownloadHandler.OnDownloadUpdated that I can see the ReceivedBytes go from zero to the TotalBytes, and InProgress being true, and then as I keep pressing F5 to continue ReceivedBytes changes back to zero, and InProgress is false, but IsComplete and IsCanceled remain false the entire time. I would have expected that once the download completed IsComplete would have been true.
I am enjoying working with CEFShap and any direction or examples that could be provided would be much appreciated.
Thanks for your time.
UPDATE 1:
The code I have tried as as follows:
NOTE: I found that after assigning a path to the callback.Continue in OBeforeDownload it fixed the IsComplete not being set to true issue. However the issue of the blank window opening remains.
In winform hosting CEFSharp I have initialised the control as follows:
browser = new ChromiumWebBrowser("https://localhost:44393/Default.aspx");
var downloadHandler = new DownloadHandler();
browser.DownloadHandler = downloadHandler;
In DownloadHandler.cs:
public class DownloadHandler : IDownloadHandler
{
public event EventHandler<DownloadItem> OnBeforeDownloadFired;
public event EventHandler<DownloadItem> OnDownloadUpdatedFired;
public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
OnBeforeDownloadFired?.Invoke(this, downloadItem);
if (!callback.IsDisposed)
{
using (callback)
{
//TODO: Detect file Type/Mime and auto download or show Save File dialogue as needed here
callback.Continue(Path.Combine(@"C:\Temp", downloadItem.SuggestedFileName), showDialog: false); // set to false so we don't show
}
}
}
public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
OnDownloadUpdatedFired?.Invoke(this, downloadItem);
if (downloadItem.IsComplete)
{
//TODO: Detect File Type/Mime and automatically open in default OS viewer
}
}
}