public static Attach GetAttach(string attachId) { Attach attach = null; try { string cmdTxt = "select * from TBL_Attachs where AttachId=@AttachId "; SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AttachId", attachId) }; DataSet ds = SqlHelper.ExecuteDataSet(DBConnectConfig.DBAttach, cmdTxt, parameters); if (ds.Tables[0].Rows.Count == 1) { DataRow r = ds.Tables[0].Rows[0]; attach = new Attach(); attach.AttachID = r["AttachID"].ToString(); attach.AttachName = r["AttachName"].ToString(); attach.UploadMode = r["UploadMode"].ToString(); attach.UploadStatus = r["UploadStatus"].ToString(); attach.ContentSize = Convert.ToInt32(r["ContentSize"]); attach.BeginDate = Convert.ToDateTime(r["BeginDate"]); attach.EndDate = (DateTime?)r["EndDate"]; attach.Segments = new List <AttachSegments>(); attach.Extension = r["ExtenSion"].ToString(); attach.SavePath = r["SavePath"].ToString(); cmdTxt = "select * from TBL_AttachSegments where attachId=@AttachId order by SerialNumber asc"; parameters = new SqlParameter[] { new SqlParameter("@AttachId", attach.AttachID) }; ds = SqlHelper.ExecuteDataSet(DBConnectConfig.DBAttach, cmdTxt, parameters); foreach (DataRow rSegment in ds.Tables[0].Rows) { attach.Segments.Add(new AttachSegments { AttachId = rSegment["AttachId"].ToString(), IpAddr = "", SegmentSize = (int)rSegment["SegmentSize"], SegmentContent = rSegment["SegmentContent"].ToString(), SerialNumber = (int)rSegment["SerialNumber"] }); } } } catch (Exception ex) { Logger.SaveLog(ex.ToString()); attach = null; } return(attach); }
/// <summary> /// 创建上传器开始上传文件 /// </summary> /// <returns></returns> public static ResponseMessage CreateUploader(string attachName, int contentSize) { ResponseMessage result = new ResponseMessage(); using (TransactionScope scope = new TransactionScope()) { try { string[] attachNameSplitArray = attachName.Split('.'); //获取后缀 string extension = attachNameSplitArray.Length > 1 ? attachNameSplitArray[attachNameSplitArray.Length - 1] : ""; //验证文件类型 if (!_uploadTypeLimit.Contains(extension)) { throw new Exception(string.Format("不允许上传{0}类型的文件", extension)); } //验证文件大小 if (contentSize > _uploadSizeLimit) { throw new Exception(string.Format("文件太大")); } Attach attach = Attach.Insert(attachName, contentSize, extension); //创建新的上传器 if (attach == null) { throw new Exception("无法获取附件信息"); } Uploader uploader = new Uploader(attach); result.errorFlag = "00"; result.errorMsg = "创建上传器成功"; result.attachId = attach.AttachID; result.uploader = uploader; scope.Complete(); } catch (Exception ex) { result.errorFlag = "01"; result.errorMsg = ex.Message; result.attachId = string.Empty; result.uploader = null; Logger.SaveLog(ex.ToString()); } } return(result); }
public static Attach Insert(string attachName, int contentSize, string extension) { Attach attach = null; try { string attachId = Guid.NewGuid().ToString(); DateTime beginDate = DateTime.Now; string uploadMode = "Segment"; string uploadStatus = "Uploading"; string cmdTxt = @"insert into TBL_Attachs(AttachId,AttachName,UploadMode,UploadStatus,ContentSize,BeginDate,IpAddr,Extension) values(@AttachId,@AttachName,@UploadMode,@UploadStatus,@ContentSize,@BeginDate,@IpAddr,@Extension) "; SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@AttachId", attachId), new SqlParameter("@AttachName", attachName), new SqlParameter("@UploadMode", uploadMode), new SqlParameter("@UploadStatus", uploadStatus), new SqlParameter("@ContentSize", contentSize), new SqlParameter("@BeginDate", beginDate.ToString()), new SqlParameter("@IpAddr", ""), new SqlParameter("@Extension", extension), }; SqlHelper.ExecuteNonQuery(DBConnectConfig.DBAttach, System.Data.CommandType.Text, cmdTxt, parameters); attach = new Attach { AttachID = attachId, AttachName = attachName, BeginDate = beginDate, ContentSize = contentSize, UploadMode = uploadMode, UploadStatus = uploadStatus, Segments = new List <AttachSegments>(), Extension = extension }; } catch (Exception ex) { attach = null; Logger.SaveLog(ex.ToString()); } return(attach); }
public Uploader(Attach attach) { this._attach = attach; this._lastAccessDate = Convert.ToDateTime(DateTime.Now.ToString()); _segmentSavePath = string.Format(@"{0}\{1}\{2}\{3}\{4}", CommonConfig.SegmentSavePath, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, attach.AttachID); if (!Directory.Exists(_segmentSavePath)) { Directory.CreateDirectory(_segmentSavePath); } _attachSavePath = string.Format(@"{0}\{1}\{2}\{3}", CommonConfig.AttachSavePath, DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); if (!Directory.Exists(_attachSavePath)) { Directory.CreateDirectory(_attachSavePath); } AddUploader(attach.AttachID, this); }