将Cefsharp中的文件下载到目录中,以允许用户打开 [英] Download files in Cefsharp to a directory allow user to open

查看:1049
本文介绍了将Cefsharp中的文件下载到目录中,以允许用户打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基本的基本Web浏览器,我的工作场所希望在该浏览器上托管一些内部应用程序。我在用C#编写的WinForms应用程序中使用 cefSharp 。我已经成功构建了浏览器来导航应用程序,但是下载处理程序遇到了麻烦。我想直接将文件下载到 C:\Users\ [username] \Downloads 文件夹(我们所有的计算机都是Windows计算机),而不必使用对话框。

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.

强制CEFSharp进行下载而不显示对话框建议使用 showDialog:false 应该可以,但是在应用此操作时,没有任何下载。同样,我也没有进行任何下载通过研究以下任何一项来取得进步:

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

作为奖励,可以选择打开文件(例如在Google Chrome浏览器中),这是很好的选择,但这并不是绝对必要的。

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.

下面的代码运行流畅,与我的尝试大致一致。本示例将打开GitHub Gist。

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



    }
}


推荐答案

在TEK的建议下,我用代码替换了问题中的 if(!callback.IsDisposed)

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

这篇关于将Cefsharp中的文件下载到目录中,以允许用户打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆