Download files in Cefsharp to a directory allow user to open

♀尐吖头ヾ 提交于 2020-08-24 08:47:10

问题


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



    }
}

回答1:


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


来源:https://stackoverflow.com/questions/42820755/download-files-in-cefsharp-to-a-directory-allow-user-to-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!