using Lskj.Business.Impl; using Lskj.Control; using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Web; using System.Windows.Forms; using Xilium.CefGlue; using Lskj.Control.Model; namespace Lskj.Control.BrowserSetting { public class BsDownloadHandler : CefDownloadHandler { private BsClient BsClient; public BsDownloadHandler(BsClient bsClient) { BsClient = bsClient; } protected override void OnBeforeDownload(CefBrowser browser, CefDownloadItem downloadItem, string suggestedName, CefBeforeDownloadCallback callback) { //callback.Continue(string.Empty, true); try { string tipMsg = string.Empty; int vsNum = 0; if (string.IsNullOrWhiteSpace(downloadItem.SuggestedFileName)) { if (BsClient != null && BsClient.frmWebBrowser != null) { BsClient.frmWebBrowser.Invoke(new Action(() => { MessageUtil.Show("当前文档类型不支持预览"); })); } if (BsClient != null && BsClient.frmWebBrowser != null && BsClient.frmWebBrowser.Parent != null && BsClient.frmWebBrowser.Parent.Name.Equals("previewFrm")) { browser.GetHost().CloseBrowser(); } else if (BsClient != null && BsClient.frmWebBrowser != null) { BsClient.frmWebBrowser.LoadUrl("file:///C:/Users/asus/Desktop/errorFileNotFound.html");//加载错误异常,文件不存在 } return; } string[] webpaths = Regex.Split(downloadItem.Url, "fileRoot/", RegexOptions.IgnoreCase); string webpath = webpaths[webpaths.Length - 1]; string szFilename = downloadItem.Url.Substring(downloadItem.Url.LastIndexOf('/') + 1);//简单处理文件名,实际中还需要单独处理,这里有BUG string SzFileName = HttpUtility.UrlDecode(szFilename); string[] szName = SzFileName.Split('/'); string fileTitle = szName[szName.Length - 1]; //downfile(e.Url, e.Filename, tempPath); if (fileTitle.Contains("?")) fileTitle = fileTitle.Substring(0, fileTitle.LastIndexOf('?')); DataRow rowItem = AttachImpl.GetTempletFile(fileTitle, webpath); string topname = downloadItem.SuggestedFileName.Substring(0, downloadItem.SuggestedFileName.LastIndexOf(".") - 1); bool isMultipleLoad = downloadItem.Url.Contains("downLoadTemp") && Regex.IsMatch(topname, @"^[+-]?\d*[.]?\d*$"); if (downloadItem.Url.StartsWith("blob")) { callback.Continue(string.Empty, true); //Download(downloadItem.Url, fileTitle); //return; } else if (rowItem != null || isMultipleLoad) { try { bool success = rowItem != null ? AttachImpl.CheckAttachOperation(2, "", Convert.ToInt32(rowItem["fileId"]), "", 0, out tipMsg) == 1 : isMultipleLoad; if (success) { Download(downloadItem.Url, fileTitle); return; } else { MessageBox.Show(tipMsg); //MessageUtil.Show(tipMsg); } } catch (Exception ex) { MessageBox.Show("下载失败!\r\n" + ex.Message); //MessageUtil.Show("下载失败!\r\n" + ex.Message); } } else { MessageBox.Show("未找到下载项"); } //browser.GetHost().CloseBrowser(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } protected override void OnDownloadUpdated(CefBrowser browser, CefDownloadItem downloadItem, CefDownloadItemCallback callback) { //if (downloadItem.IsComplete) //{ // MessageBox.Show("下载成功"); // if (browser.IsPopup && !browser.HasDocument) // { // //browser.GetHost().ParentWindowWillClose(); // browser.GetHost().CloseBrowser(); // } //} //downloadItem.IsComplete //if (downloadItem.IsComplete) { // string tipMsg = string.Empty; int vsNum = 0; // DataRow rowItem = AttachImpl.GetTempletFile(downloadItem.SuggestedFileName); // string topname = downloadItem.SuggestedFileName.Substring(0, downloadItem.SuggestedFileName.LastIndexOf(".") - 1); // bool isMultipleLoad = downloadItem.Url.Contains("downLoadTemp") && Regex.IsMatch(topname, @"^[+-]?\d*[.]?\d*$"); // if (rowItem != null || isMultipleLoad) // { // try // { // bool success = rowItem != null ? AttachImpl.CheckAttachOperation(2, "", Convert.ToInt32(rowItem["fileId"]), "", 0, out tipMsg) == 1 : isMultipleLoad; // if (success) // { // string szFilename = downloadItem.Url.Substring(downloadItem.Url.LastIndexOf('/') + 1);//简单处理文件名,实际中还需要单独处理,这里有BUG // string SzFileName = HttpUtility.UrlDecode(szFilename); // string[] szName = SzFileName.Split('/'); // //downfile(e.Url, e.Filename, tempPath); // Download(downloadItem.Url, szName[szName.Length - 1]); // return; // } // else // { // MessageUtil.Show(tipMsg); // } // } // catch (Exception ex) // { // MessageUtil.Show("下载失败!\r\n" + ex.Message); // } // } // else // { // MessageUtil.Show("未找到下载项"); // } // //browser.GetHost().CloseBrowser(); //} } /// /// 说明:定义服务器文件地址,含文件名。客户端接收地址及文件名由客户端自行定义 /// 创建人:王一帆 /// 创建日期: /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The URL. /// Name of the file. public static void Download(string URL, string FileName) { FrmSelectDownload frmSelectDownload = new FrmSelectDownload(); frmSelectDownl