using CefSharp; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Lecall { internal class DownloadHandler : IDownloadHandler { //public event EventHandler OnBeforeDownloadFired; //public event EventHandler 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.Url+ downloadItem.SuggestedFileName, true); //callback.Continue(downloadItem.SuggestedFileName, true ); FrmDownLoad myfrm = new FrmDownLoad(); myfrm.originalurl = downloadItem.Url; myfrm.filename = downloadItem.SuggestedFileName; myfrm.Show(); //DownloadFile(downloadItem.OriginalUrl, @"C:\123.rar"); } } } public bool OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { return false; //var handler = OnDownloadUpdatedFired; //if (handler != null) //{ // handler(this, downloadItem); //} } public void OnDownloadUpdated(IWebBrowser browserControl, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) { //var handler = OnDownloadUpdatedFired; //if (handler != null) //{ // handler(this, downloadItem); //} } public void OnBeforeDownload(IWebBrowser browserControl, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { //var handler = OnDownloadUpdatedFired; //if (handler != null) //{ // handler(this, downloadItem); //} if (!callback.IsDisposed) { using (callback) { //callback.Continue(downloadItem.Url+ downloadItem.SuggestedFileName, true); //callback.Continue(downloadItem.SuggestedFileName, true ); FrmDownLoad myfrm = new FrmDownLoad(); myfrm.originalurl = downloadItem.Url; myfrm.filename = downloadItem.SuggestedFileName; myfrm.Show(); //DownloadFile(downloadItem.OriginalUrl, @"C:\123.rar"); } } } /// /// c#,.net 下载文件 /// /// 下载文件地址 /// /// 下载后的存放地址 /// 用于显示的进度条 /// public void DownloadFile(string URL, string filename) { float percent = 0; try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; #region 保存文件对话框 SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //设置文件类型 saveFileDialog1.Filter = " xls files(*.xls)|*.txt|All files(*.*)|*.*"; //设置文件名称: saveFileDialog1.FileName = filename; //设置默认文件类型显示顺序 saveFileDialog1.FilterIndex = 2; //保存对话框是否记忆上次打开的目录 saveFileDialog1.RestoreDirectory = true; //点了保存按钮进入 if (saveFileDialog1.ShowDialog() == DialogResult.OK) { } else { return; } #endregion System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 } so.Close(); st.Close(); } catch (System.Exception) { throw; } } } }