using CommonLib;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;
using Xilium.CefGlue;
namespace Lskj.MyControl.BrowserSetting
{
public class BsDownloadHandler : CefDownloadHandler
{
public bool isfirstDownLoad = true;
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))
{
browser.GetHost().CloseBrowser();
return;
}
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 = GetTempletFile(fileTitle);
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"))
{
Download(downloadItem.Url, fileTitle);
return;
}
else if (rowItem != null || isMultipleLoad)
{
try
{
bool success = rowItem != null ? 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();
frmSelectDownload.URL = URL;
frmSelectDownload.FileName = FileName;
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();
//}
}
///
/// 说明:根据附件名字获取附件
/// 创建人:王一帆
/// 创建日期:2019-12-13
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// DataTable.
public static DataRow GetTempletFile(string sName)
{
string sqlValue = "select * from P_fm_FileTab where sName=@sName";
DataTable dt = SqlHelper.ExecuteDataTable(sqlValue, new SqlParameter[] { new SqlParameter("@sName", sName) });
return dt.Rows.Count > 0 ? dt.Rows[0] : null;
}
///
/// 说明:
/// 创建人:龚宇超
/// 创建日期:2018-01-17
/// 修改人:
/// 修改日期:
/// 修改备注:
/// 版本:1.0
///
/// 1=上传,2=下载,3=删除,4=锁定,5=解除锁定 必填
/// The species no.
/// The file id.
/// Name of the file.
/// Size of the field.
/// 返回提示信息
/// System.Int32.
///
public static int CheckAttachOperation(int operType, string speciesNo, int fileId, string fileName, int fileSize, out string tipMsg)
{
try
{
SqlParameter pMsg = new SqlParameter("@msg", SqlDbType.VarChar, 2000);
pMsg.Direction = ParameterDirection.Output;
SqlParameter returnValue = new SqlParameter("@return", SqlDbType.Int, 4);
returnValue.Direction = ParameterDirection.ReturnValue;
SqlParameter[] param =
{
new SqlParameter("@opertype",SqlDbType.Int),
new SqlParameter("@operatorid",SqlDbType.Int),
new SqlParameter("@operatorname",SqlDbType.VarChar,50),
new SqlParameter("@fileSpecisNo",SqlDbType.VarChar,50),
new SqlParameter("@fileid",SqlDbType.Int),
new SqlParameter("@filename",SqlDbType.VarChar,200),
new SqlParameter("@filesize",SqlDbType.Int),
pMsg,
returnValue
};
param[0].Value = operType;
param[1].Value = ERPInfo.Instance.EmployeeId;
param[2].Value = ERPInfo.Instance.EmployeeName;
param[3].Value = speciesNo;
param[4].Value = fileId;
param[5].Value = fileName;
param[6].Value = fileSize;
DataSet ds = SqlHelper.ExecuteDataSet(CommandType.StoredProcedure, "p_SystemAttachOperation", "attach", param);
tipMsg = pMsg.Value + "";
return Convert.ToInt32(returnValue.Value + "");
}
catch (Exception)
{
}
tipMsg = string.Empty;
return 1;
}
}
}