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; using System.Runtime.InteropServices; namespace Lskj.Control.BrowserSetting { public class BsDownloadHandler : CefDownloadHandler { [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr GetParent(IntPtr hWnd); 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")) { BsClient.frmWebBrowser.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) { string ModuleCode = string.Empty; GetParentForm(browser, out ModuleCode); Download(downloadItem.Url, fileTitle, ModuleCode); 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,string ModuleCode) { FrmSelectDownload frmSelectDownload = new FrmSelectDownload(); frmSelectDownload.URL = URL; frmSelectDownload.FileName = FileName; frmSelectDownload.ModelId = ModuleCode; frmSelectDownload.Show(); //SaveFileDialog saveFileDialog = new SaveFileDialog(); //saveFileDialog.FileName = FileName; //saveFileDialog.Title = "下载模板文件到"; //saveFileDialog.RestoreDirectory = true; ////点了保存按钮进入 //if (saveFileDialog.ShowDialog() == DialogResult.OK) //{ // string localFilePath = System.IO.Path.GetFullPath(saveFileDialog.FileName);//获得文件路径,含文件名 // string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径 // string FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\"));//获取文件路径,不带文件名 // FrmDownload frm = new FrmDownload(URL, FileName, localFilePath); // frm.Show(); //} } /// /// 通过 CefBrowser 获取所在的 WinForms 窗口 /// public static Form GetParentForm(CefBrowser browser,out string ModuleCode) { ModuleCode = ""; try { if (browser == null) return null; IntPtr browserHandle; using (var browserHost = browser.GetHost()) { browserHandle = browserHost.GetWindowHandle(); } if (browserHandle == IntPtr.Zero) return null; IntPtr parentHandle = GetParent(browserHandle); System.Windows.Forms.Control control = null; while (parentHandle != IntPtr.Zero && control == null) { // The CEF child now sits inside an application-owned native // host. Walk through that host to preserve the original // FrmWebBrowser-based module lookup. control = System.Windows.Forms.Control.FromHandle(parentHandle); parentHandle = GetParent(parentHandle); } if (control == null) return null; if (control != null && control.Parent != null && control.Parent.Parent != null) { ModuleCode = (control.Parent.Parent).Tag + ""; } return control as Form; } catch (Exception ex) { } return null; } } }