Download files in Cefsharp to a directory allow user to open
Asked Answered
R

1

7

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:

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);
        }



    }
}
Redingote answered 15/3, 2017 at 21:11 Comment(4)
If downloadItem.SuggestedFileName is just a string that represents a path, can you not just replace it with C:\Users\[username]\Downloads and then set showDialog: false? So, in your case, it would be callback.Continue("C:\Users\[username]\Downloads", showDialog: false);.Chenopod
That previous comment should be: callback.Continue($@"C:\Users\[username]\Downloads\{downloadItem.SuggestedFileName}", showDialog: false); Note, though, you will need to implement some logic to prevent file name clashes.Chenopod
oy, talk about an answer staring you in the face.Redingote
One of those days, eh? ;-)Chenopod
R
2

On TEK's advice, I replaced the if(!callback.IsDisposed) block in the question with the code below.

if (!callback.IsDisposed)
                {
                    using (callback)
                    {
                        callback.Continue(@"C:\Users\" + 
                                System.Security.Principal.WindowsIdentity.GetCurrent().Name. + 
                                @"\Downloads\" + 
                                downloadItem.SuggestedFileName, 
                            showDialog: false);
                    }
                }
Redingote answered 16/3, 2017 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.