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 upLoadLiuyangFile { /// /// 上传一个文件页面,并保存,包括修修 /// /// postFileUI对象 /// 客户文件表关联 /// 合同文件ID /// 先判决是否存在CountractFileKeiId 如果不存在,则根据CorporateClientKeyid 新建 这两个ID不能同时为null public void SavAndUpDataContractFile(HttpPostedFile filel, EC_OrderLiuyang 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],[orderid],[FileName],[FilePath],[Remark],[Creater],[CreateTime],[Updater],[LastUpdateTime] FROM [ECTEST].[dbo].[EC_OrderLiuyang] where Keyid='{0}'", CountractFileKeiId.ToString()); //开始修改文件 SqlCommand myCmd = new SqlCommand(sqlStr, DC.Connection); var a = myCmd.ExecuteReader(); EC_OrderLiuyang Contract1 = new EC_OrderLiuyang(); while (a.Read()) { //注意如何将每次读取到的记录添加到listbox1中(因为第一次做时出错了,嘿嘿) Contract1.Keyid = a.GetGuid(0); Contract1.orderid = a.GetInt32(1); Contract1.FileName = a.GetString(2); Contract1.FilePath = a.GetString(3); Contract1.Remark = a.GetString(4); } a.Close(); //关闭sqldatareader UploadFileTIhuan(filel, Contract1.FilePath); //直接替换文件 } else { //不是修改,则增加 //修改文件 if(Contract.orderid==0) { throw new Exception("订单ID为空,不能留样"); } //开始上传合同 try { FilePath= UploadFile(filel, "LiuyangIMG", Contract.FileName); //上传文件 } catch (Exception e) { throw e; } //写入数据库 sqlStr = string.Format(@" INSERT INTO [dbo].[EC_OrderLiuyang] ( [Keyid],[orderid],[FileName],[FilePath],[Remark],[Creater],[CreateTime] ) VALUES('{0}' ,'{1}' ,'{2}' ,'{3}' ,'{4}' ,'{5}' ,'{6}' )", Guid.NewGuid().ToString(), Contract.orderid , Contract.FileName, 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) { sqlStr = string.Format(@" update [EC_OrderBasic] set [LiuyangStatus] = 1 where [Keyid]= {0} ", Contract.orderid); myCmd = new SqlCommand(sqlStr, DC.Connection); a = myCmd.ExecuteNonQuery(); 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(); } } /// /// 直接替换文件 /// /// postFileUI对象 /// /UpFile+文件路径 /// 文件名 /// 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; } } /// /// 上传文件 /// /// postFileUI对象 /// /UpFile+文件路径 /// 文件名 /// 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; } } } }