CEfSharp download the file save pop-up box, to achieve IDownloadHandler Interface

Previous section talked about how CefSharp integrated into C #, but integrated web interface will link into ChromiumWebBrowser, but download the attachment functionality on the web interface to do so slightly.

Baidu for a long time or did not get, only to see the official website of the Excample and source code, and finally hit a dead rat blind cat to get, we need to implement an interface.

public RevenueContractFrmWeb(Adapter adapter)
        {
            Adapter = adapter;
            InitializeComponent();
            //string url = GlobalDefination.Urlex + "/go-logistics-client/inAndOutPages/incomeContract/contractMg/ContractMain.jsp"; 
            //webBrowser.Navigate(url+"?sessionId=" + adapter.SessionId);

            string url =  GlobalDefination.Urlex + "/go-logistics-client/inAndOutPages/incomeContract/contractMg/ContractMain.jsp?sessionId=" + adapter.SessionId;
            CefSharp.WinForms.ChromiumWebBrowser wb = new CefSharp.WinForms.ChromiumWebBrowser(url);
            wb.Dock = DockStyle.Fill;
            this.Controls.Add(wb); 
            wb.DownloadHandler = new MyDownloadHandler();
        }

Implementation class

 internal class MyDownloadHandler : IDownloadHandler
        {

            public bool OnBeforeDownload(CefSharp.DownloadItem downloadItem, out string downloadPath, out bool showDialog)
            {
                downloadPath ="";
                showDialog = true;
                return true;
            }
            public bool OnDownloadUpdated(CefSharp.DownloadItem downloadItem)
            {
                return false;
            }
        }

 

 

Source: https://blog.csdn.net/weiyongliang_813/article/details/50575706

=====================================================================================================

CEfSharp download the file save pop-up box IDownloadHandler, Winfrom embedded web page export function response

 using CefSharp;

 public ChromiumWebBrowser browser;
 NativeAppJsObjBasic NativeAppJsObj;

// form part 
Private  void FrmWebView_Load ( Object SENDER, EventArgs E)
        {
 
            browser = new ChromiumWebBrowser(URL);

            panel1.Controls.Add(browser);
            browser.Dock = DockStyle.Fill;
            NativeAppJsObj = new NativeAppJsObjBasic();
            browser.RegisterJsObject("NativeAppJsObj", NativeAppJsObj, true);
            NativeAppJsObj.OnSessionExpired += NativeAppJsObj_OnSessionExpired;
            browser.IsBrowserInitializedChanged += Browser_IsBrowserInitializedChanged;
            browser.DownloadHandler = new MyDownLoadFile();

        }


    ///  <Summary> 
    /// response download file compatible
     ///  </ Summary> 
    public  class MyDownLoadFile: IDownloadHandler
    {
        public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    callback.Continue(@"C:\Users\" +
                            System.Security.Principal.WindowsIdentity.GetCurrent().Name +
                            @"\Downloads\" +
                            downloadItem.SuggestedFileName,
                        showDialog: true);
                }
            }
        }

        public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            //downloadItem.IsCancelled = false;
        }
        public bool OnDownloadUpdated(CefSharp.DownloadItem downloadItem)
        {
            return false;
        }
    }

 

 

Source: https://blog.csdn.net/qq_41715982/article/details/97284255

=====================================================================================================

If you want to download IE is used to download, you can refer to the following method:

public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
{
      WebBrowser ie = new WebBrowser();
      ie.Navigate(downloadItem.Url);
}

 

Original link: https://blog.csdn.net/CFY530/article/details/81145875

=====================================================================================================

Have been doing lately CEFsharp aspects encountered can not save downloaded files problems. Record resolved. Mainly applied
IDownloadHandler
call the method:
web.MenuHandler = new new MyWebContextMenu ();
web.DownloadHandler = new new MyDownLoadFile ();

IDownloadHandler.cs file

    ///  <the Summary> 
    /// download file
     ///  </ the Summary> 
    public  class MyDownLoadFile: IDownloadHandler
    {
        public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    callback.Continue(@"C:\Users\" +
                            System.Security.Principal.WindowsIdentity.GetCurrent().Name+
                            @"\Downloads\" +
                            downloadItem.SuggestedFileName,
                        showDialog: true);
                }
            }
        }
 
        public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            //downloadItem.IsCancelled = false;
        }
        public bool OnDownloadUpdated(CefSharp.DownloadItem downloadItem)
        {
            return false;
        }
    }

 

 

Source: https://blog.csdn.net/u010919083/article/details/78342503

=====================================================================================================

CEF download is very easy to expand, it provides a rich interface and control functions, such as the file is being downloaded to pause and continue, cancel and other operations. And CEF also help us implement a default Save As dialog box, if it is not necessary that you do not even need to own to achieve the Save dialog box. Next we look at CEF (for example using cefclient project) for the two interfaces download functions provided

Inheritance CefDownloadHandler

Before everything started, the first thing you want to inherit CefDownloadHandler ClientHandler categories:

This class provides two interfaces, respectively, OnBeforeDownloadand OnDownloadUpdatedthe former is the task before the download will begin interface is a callback, you need to implement some pre-processing operation in the interface according to your needs. The latter is the task of the download process callback interface, including the progress of the task, status, and control functions. The following describes in detail two interfaces.

OnBeforeDownload Interface

virtual void OnBeforeDownload(
      CefRefPtr<CefBrowser> browser,
      CefRefPtr<CefDownloadItem> download_item,
      const CefString& suggested_name,
      CefRefPtr<CefBeforeDownloadCallback> callback) = 0;

The above introduction to the interface will be called before files start the download,

  • parameter indicates the current browser instances
  • download_item contains status information of the download task, through its member functions IsInProgress, IsComplete, IsCanceledand other methods to determine the current state of the file, but the file before downloading to judge these seemingly does not make sense.
  • suggested_name represents the current CEF good advice to help you save set name, the general will intercept the file path of the last file name as the suggested name.
  • This callback parameter is more important, when you call a callback method Continue, the task will start the download, the first callback parameter download_pathname is to save the file, the second parameter indicates whether the Save dialog box pops up, when the second when the parameter is set to true, CEF will help us to pop up a save File save dialog. If you set to false, then save dialog will not pop up and automatically save files to the first path parameter set.

Through this interface, we can do some pre-processing tasks, such as when you want to create a download task in the interface, you can download_itemtask ID (GetId method) to the UI to represent the task is about to begin acquisition parameters. Next, enter the following download task status updating function, and then to update the contents of the interface according to the task Id.

OnDownloadUpdated Interface

The interface is a callback download process in the task, but as far as I tested, this interface will give priority to OnBeforeDownload interface is called, the specific reasons still do not quite understand, but harmless, here we are only concerned with the progress of the task, status and control functions on it .

virtual void OnDownloadUpdated(CefRefPtr<CefBrowser> browser,
                                 CefRefPtr<CefDownloadItem> download_item,
                                 CefRefPtr<CefDownloadItemCallback> callback) {}
  • This little introduction browser
  • Download_item as described above, the progress of the task can get, status, download speed, and so on has been downloaded, you can look CefDownloadItem interface has a look at what control functions.
  • Here's callback and callback above callback interface is not the same, this callback can pause control tasks, continue, and stop, you can maintain a list of current tasks and tasks of the callback Id binding, when the interface came pause control messages mission, the callback can be found by the task Id to the list, call the callback method pause to pause task. Of course, do not forget to remove the binding relationship between these callback and tasks Id from the list in the task is completed or canceled time.

to sum up

Download interface control functions provided by CEF is still very rich, and even download speeds are in progress to help you complete the calculation you can use directly. If you want to achieve some UI with personalized display it can all be done. You can try it for yourself, experience any technical questions please discuss below.

 

Source: https://www.mycode.net.cn/language/cpp/2627.html

=====================================================================================================

 

Guess you like

Origin www.cnblogs.com/mq0036/p/11932228.html