CefSharp 實現下載功能
阿新 • • 發佈:2022-11-29
CefSharp 能 在C# winform 中實現 瀏覽器 功能
同樣的在瀏覽器中 網頁上如果有 下載 按鈕 實現下載功能
直接上程式碼
CefSharp 在winform上實現 瀏覽器功能
using System; using System.Text; using System.Windows.Forms; using CefSharp; using CefSharp.WinForms;
public ChromiumWebBrowser browser; public void InitBrowser() { //Cef.Initialize(new CefSettings());browser = new ChromiumWebBrowser("https://ai.12348.gov.cn/pc/"); browser.LifeSpanHandler = new LifeSpanHandler();//超連結不彈新窗體 browser.DownloadHandler = new DownloadHandler();//下載 this.panel1.Controls.Add(browser);//panel }
重點: 建立CustomDownloadHandler類並實現IDownloadHandler介面
建立 DownloadHandler.cs 檔案
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; usingSystem.IO; namespace LawSever { 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) { //throw new NotImplementedException(); var handler = OnBeforeDownloadFired; if (handler != null) { handler(this, downloadItem); } if (!callback.IsDisposed) { //using (callback) //{ // callback.Continue(downloadItem.SuggestedFileName, showDialog: true); //} using (callback) {
//好用 downloadItem.SuggestedFileName為下載的檔名 //callback.Continue(@"C:\Users\" + // System.Security.Principal.WindowsIdentity.GetCurrent().Name + @"\Downloads\" + // downloadItem.SuggestedFileName, // showDialog: false); string strDownloadFile = @"c:\DownloadFile\file.docx";//手動指定下載目錄和下載檔名 callback.Continue(@strDownloadFile,showDialog: false); } } } public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { //下載過程中 呼叫多次 } public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { //沒有被呼叫 } public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { //沒有被呼叫 //var handler = OnDownloadUpdatedFired; //if (handler != null) //{ // handler(this, downloadItem); //} }
} }
超連結不彈新窗體
建立 LifeSpanHandler.cs 檔案
using CefSharp; using CefSharp.WinForms; namespace LawSever { internal class LifeSpanHandler : ILifeSpanHandler { //彈出前觸發的事件 public bool OnBeforePopup(IWebBrowser webBrowser, IBrowser browser, IFrame frame, string targetUrl, string targetFrameName, WindowOpenDisposition targetDisposition, bool userGesture, IPopupFeatures popupFeatures, IWindowInfo windowInfo, IBrowserSettings browserSettings, ref bool noJavascriptAccess, out IWebBrowser newBrowser) { //使用源視窗開啟連結,取消建立新視窗 newBrowser = null; var chromiumWebBrowser = (ChromiumWebBrowser)webBrowser; chromiumWebBrowser.Load(targetUrl); return true; } public void OnAfterCreated(IWebBrowser chromiumWebBrowser, IBrowser browser) { } public bool DoClose(IWebBrowser chromiumWebBrowser, IBrowser browser) { return true; } public void OnBeforeClose(IWebBrowser chromiumWebBrowser, IBrowser browser) { } } }
感謝:
https://www.javaroad.cn/questions/288431
https://www.cnblogs.com/mchao/p/13935777.html