using CY.Infrastructure.Logging;
|
using CY.Model.OA;
|
using CY.SQLDAL;
|
using System;
|
using System.Collections.Generic;
|
using System.Data.SqlClient;
|
using System.IO;
|
using System.Linq;
|
using System.Web;
|
|
namespace CY.WebForm.Helper
|
{
|
public class upLoadContractFile
|
{
|
|
|
|
/// <summary>
|
/// 上传一个文件页面,并保存,包括修修
|
/// </summary>
|
/// <param name="filel">postFileUI对象</param>
|
/// <param name="Contract">客户文件表关联 </param>
|
/// <param name="CountractFileKeiId">合同文件ID</param>
|
/// <remarks>先判决是否存在CountractFileKeiId 如果不存在,则根据CorporateClientKeyid 新建 这两个ID不能同时为null </remarks>
|
public void SavAndUpDataContractFile(HttpPostedFile filel, OA_CorporateClientsContract Contract = null, Guid? CountractFileKeiId = null)
|
{
|
if (Contract == null && CountractFileKeiId == null)
|
{
|
throw new Exception("客户ID和合同ID同时为空,不能修改或者更新合同字段");
|
|
}
|
Database DC = new Database();
|
string FilePath = "";
|
try
|
{
|
String sqlStr = "";
|
|
if (CountractFileKeiId != null)
|
{
|
//修改文件
|
sqlStr = string.Format(@"SELECT [Keyid] ,[FileName] ,[PageCode] ,[FilePath] ,[Remark] FROM [ECTEST].[dbo].[OA_CorporateClientContract] where Keyid='{0}'", CountractFileKeiId.ToString());
|
//开始修改文件
|
SqlCommand myCmd = new SqlCommand(sqlStr, DC.Connection);
|
|
var a = myCmd.ExecuteReader();
|
|
OA_CorporateClientsContract Contract1 = new OA_CorporateClientsContract();
|
while (a.Read())
|
{ //注意如何将每次读取到的记录添加到listbox1中(因为第一次做时出错了,嘿嘿)
|
Contract1.Keyid = a.GetGuid(0);
|
|
Contract1.FileName = a.GetString(1);
|
Contract1.PageCode = a.GetInt32(2);
|
Contract1.FilePath = a.GetString(3);
|
Contract1.Remark = a.GetString(4);
|
}
|
|
a.Close(); //关闭sqldatareader
|
|
UploadFileTIhuan(filel, Contract1.FilePath); //直接替换文件
|
|
|
|
|
}
|
else
|
{
|
//不是修改,则增加
|
|
//修改文件
|
|
if(Contract.CorporateClientsid==null)
|
{
|
throw new Exception("客户ID为空,不能创建合同");
|
}
|
//开始上传合同
|
try
|
{
|
FilePath= UploadFile(filel, "ClientContractIMG", Contract.FileName); //上传文件
|
}
|
catch (Exception e)
|
{
|
|
throw e;
|
}
|
//写入数据库
|
sqlStr = string.Format(@"USE [ECTEST] INSERT INTO [dbo].[OA_CorporateClientContract] ([Keyid] ,[CorporateClientsid] ,[CorporateClientsName] ,[FileName] ,[PageCode] ,[FilePath] ,[Remark] ,[Creater] ,[CreateTime] ) VALUES('{0}' ,'{1}' ,'{2}' ,'{3}' ,'{4}' ,'{5}' ,'{6}' ,'{7}' ,'{8}')",Guid.NewGuid().ToString(), Contract.CorporateClientsid.ToString(),"", Contract.FileName, Contract.PageCode, FilePath, Contract.Remark, Contract.Creater,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
try
|
{
|
SqlCommand myCmd = new SqlCommand(sqlStr, DC.Connection);
|
|
var a = myCmd.ExecuteNonQuery();
|
if (a == 1)
|
return;
|
else
|
{
|
//删除文件?
|
}
|
|
}
|
catch (Exception e)
|
{
|
//删除文件?
|
throw e;
|
}
|
//开始修改文件
|
|
|
|
|
|
}
|
|
}
|
catch (Exception ee)
|
{
|
new Log4NetAdapter().Log("文件上传 错误:" + ee.Message);
|
|
}
|
finally
|
{
|
if (DC.Connection.State != System.Data.ConnectionState.Closed)
|
DC.Connection.Close();
|
}
|
}
|
|
|
|
|
|
/// <summary>
|
/// 直接替换文件
|
/// </summary>
|
/// <param name="filel"> postFileUI对象</param>
|
/// <param name="Filepath"> /UpFile+文件路径</param>
|
/// <param name="fileName"> 文件名</param>
|
/// <returns></returns>
|
public string UploadFileTIhuan(HttpPostedFile filel, string fileName)
|
{
|
try
|
{
|
string DirPath = System.Web.HttpContext.Current.Request.MapPath(fileName);
|
|
|
|
|
|
filel.SaveAs(DirPath );
|
|
return fileName; //返回相对路径
|
|
}
|
catch (Exception e)
|
{
|
|
throw e;
|
}
|
|
}
|
|
|
|
|
/// <summary>
|
/// 上传文件
|
/// </summary>
|
/// <param name="filel"> postFileUI对象</param>
|
/// <param name="Filepath"> /UpFile+文件路径</param>
|
/// <param name="fileName"> 文件名</param>
|
/// <returns></returns>
|
public string UploadFile(HttpPostedFile filel, string Filepath,string fileName)
|
{
|
try
|
{
|
string DirPath = System.Web.HttpContext.Current.Request.MapPath("/UpFile");
|
|
if (Directory.Exists(DirPath))
|
{
|
//文件夹已经存在
|
}
|
else
|
{
|
try
|
{
|
Directory.CreateDirectory(DirPath);
|
//创建成功
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
|
}
|
}
|
//创建第二级
|
if (Directory.Exists(DirPath+"/"+ Filepath))
|
{
|
//文件夹已经存在
|
}
|
else
|
{
|
try
|
{
|
Directory.CreateDirectory(DirPath + "/" + Filepath);
|
//创建成功
|
}
|
catch (Exception ex)
|
{
|
throw ex;
|
|
}
|
}
|
string File = Filepath + "/" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss") + fileName;
|
|
filel.SaveAs(DirPath+ "/"+File);
|
|
return "/UpFile/"+File; //返回相对路径
|
|
}
|
catch (Exception e)
|
{
|
|
throw e;
|
}
|
|
}
|
|
}
|
}
|