0

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.

enter image description here

I want to do the following:

  1. Not have the blank window open.

  2. 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.

  3. 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:

  1. The blank window still opens and stays open.

  2. 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
        }
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • What have you tried? Please edit your question and provide the code you have so far. – amaitland Sep 09 '20 at 03:27
  • @amaitland thanks for the response. I will do one better, I will create a demo project and update the post with it. Stay tuned ;-) – Scotty Laughton Sep 09 '20 at 04:27
  • Great. No zip files please, I review 99% of code on an Android tablet. GitHub, Bitbucket, etc are preferable. – amaitland Sep 09 '20 at 04:54
  • @amaitland, ah.... ok, I just created a fully working VS2019 project complete with website to serve up the docx file where you could actually replicate the issue. No matter i will just put code samples in the post instead. I see you in some posts at magpcss.org. I tried to register but I don't get the confirmation email, not in spam either, and in their FAQ they say if this happens, "...try contacting an administrator...", but there is no admin contact link. – Scotty Laughton Sep 09 '20 at 06:09
  • @amaitland ok I have added code sample – Scotty Laughton Sep 09 '20 at 06:22
  • You can upload your solution to `Github` (free for open source projects). Your link specifies target _blank so it's opening a popup as requested. If you don't need to have a popup in general then changing your html would be simplest otherwise you need to close the popup see https://magpcss.org/ceforum/viewtopic.php?f=6&t=16516#p40729 – amaitland Sep 09 '20 at 06:39
  • @amaitland ok so it is the _BLANK causing the popup. The site needs to work in both normal desktop browsers and in CEFSharp. It is a shame there is no way to say don't show the popup. I wonder if I can convert the popup to a new tab, that would be less in the users face I guess. – Scotty Laughton Sep 09 '20 at 06:55
  • Tabs are more complex as you would have to implement your own tab control, there is no built in support. You can cancel the popup, you would then have to manually download the file. Which is easy enough. It really depends on your use case. If your unfamiliar with the target attribute then best to read https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a – amaitland Sep 09 '20 at 06:59
  • @amaitland ok cool. I am using EasyTabs. Maybe the best option is cancelling the popup and downloading the file myself. So is the correct way to cancel the popup just to set the newBrowser parameter of OnBeforePopup to null, and then call my own async code from there to download the file and open in the default viewer? – Scotty Laughton Sep 09 '20 at 07:07
  • If your site doesn't need authentication then that's probably ok. Best to spawn a new Task to perform the download or you can call http://cefsharp.github.io/api/84.4.x/html/M_CefSharp_IBrowserHost_StartDownload.htm to trigger the download and use the same `IDownloadHandler` implementation as you would normally. – amaitland Sep 09 '20 at 07:15
  • @amaitland thank you for taking the time to answer my questions. I have one more. When I create a class that inherits from ILifeSpanHandler, I have to implement DoClose, OnAfterCreated, OnBeforeClose and OnBeforePopup. I am only interested in the last one. Do I just leave all the others blank, or do I need to be doing some base implementation of some sort? – Scotty Laughton Sep 09 '20 at 07:28
  • If the method returns void then do nothing, if it returns a bool then the default is `return false`. It's an interface, so there's no base implementation. – amaitland Sep 09 '20 at 07:56

0 Answers0