private UpLoadFileResult UpLoadPicture(System.IO.Stream file, string dirPath, string fileName = "") { UpLoadFileResult upLoadFileResult = new UpLoadFileResult(); string encodingName = "utf-8"; string imgType = string.Empty; List <string> typeList = new List <string>() { "JPG", "BMP", "PNG", "JPEG" }; using (MemoryStream ms = new MemoryStream()) { file.CopyTo(ms); ms.Position = 0; var encoding = Encoding.GetEncoding(encodingName); var reader = new StreamReader(ms, encoding); var headerLength = 0L; //读取第一行 var firstLine = reader.ReadLine(); //计算偏移(字符串长度+回车换行2个字符) headerLength += encoding.GetBytes(firstLine).LongLength + 2; //读取第二行 var secondLine = reader.ReadLine(); //计算偏移(字符串长度+回车换行2个字符) headerLength += encoding.GetBytes(secondLine).LongLength + 2; //解析文件名 string orgFileName = new System.Text.RegularExpressions.Regex("filename=\"(?<fn>.*)\"").Match(secondLine).Groups["fn"].Value; if (string.IsNullOrEmpty(fileName)) { fileName = orgFileName; } else { fileName = orgFileName.Replace(orgFileName.Substring(0, orgFileName.LastIndexOf('.')), fileName); } //判断图片格式 imgType = fileName.Substring(fileName.LastIndexOf('.') + 1).ToUpper(); if (!typeList.Contains(imgType)) { upLoadFileResult.FileName = "errorType"; upLoadFileResult.success = false; return(upLoadFileResult); } //一直读到空行为止 while (true) { //读取一行 var line = reader.ReadLine(); //若到头,则直接返回 if (line == null) { break; } //若未到头,则计算偏移(字符串长度+回车换行2个字符) headerLength += encoding.GetBytes(line).LongLength + 2; if (line == "") { break; } } //设置偏移,以开始读取文件内容 ms.Position = headerLength; ////减去末尾的字符串:“/r/n--/r/n” ms.SetLength(ms.Length - encoding.GetBytes(firstLine).LongLength - 3 * 2); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } string path = Path.Combine(dirPath, fileName); using (FileStream fileToupload = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read)) { ms.CopyTo(fileToupload); fileToupload.Flush(); } upLoadFileResult.FileName = fileName; upLoadFileResult.success = true; } return(upLoadFileResult); }
public UpLoadFileResult UpLoadFile1(HttpPostedFile file, string progId = "") { UpLoadFileResult upLoadFileResult = new UpLoadFileResult(); bool isLocalHost = true; string fileName = LibDateUtils.Now().Ticks.ToString(); fileName = file.FileName.Replace(file.FileName.Substring(0, file.FileName.LastIndexOf('.')), fileName); string filePath = Path.Combine(EnvProvider.Default.RuningPath, "TempData", "ImportData", fileName); string url = Path.Combine("\\TempData", "ImportData", fileName); #region 对应实体图片路径 if (!string.IsNullOrEmpty(progId)) { string sql = string.Format(" SELECT Url,LocalHost FROM ComImagePath WHERE Progid='{0}' ", progId); DataSet dataSet = new LibDataAccess().ExecuteDataSet(sql); if (dataSet != null && dataSet.Tables.Count > 0 && dataSet.Tables[0].Rows.Count > 0) { DataRow dr = dataSet.Tables[0].Rows[0]; isLocalHost = LibSysUtils.ToBoolean(dr["LocalHost"]); url = LibSysUtils.Combine(LibSysUtils.ToString(dr["Url"]), fileName); filePath = LibSysUtils.Combine(EnvProvider.Default.RuningPath, url); } } #endregion #region 新增图片实体 LibEntryParam entryParam = new LibEntryParam(); DataSet dataset = null; LibBcfData bcfData = (LibBcfData)LibBcfSystem.Default.GetBcfInstance("KanTime.Picture"); dataset = bcfData.AddNew(entryParam); #region 填充数据 dataset.EnforceConstraints = false; try { #region 表头 DataRow masterRow = dataset.Tables[0].Rows[0]; masterRow.BeginEdit(); masterRow["Name"] = fileName; masterRow["Url"] = url; masterRow.EndEdit(); #endregion #region 明细 //dataset.Tables[1].BeginLoadData(); //dataset.Tables[1].EndLoadData(); #endregion } finally { dataset.EnforceConstraints = true; } dataset = bcfData.InnerSave(BillAction.AddNew, null, dataset); if (bcfData.ManagerMessage.IsThrow) { StringBuilder strMsg = new StringBuilder(); foreach (var item in bcfData.ManagerMessage.MessageList) { strMsg.AppendFormat("{0}", item.Message); } throw new Exception(strMsg.ToString()); } #endregion #endregion file.SaveAs(filePath); #region 返回本机或外网地址 if (isLocalHost) { upLoadFileResult.FileName = url; } else { upLoadFileResult.FileName = LibSysUtils.Combine(EnvProvider.Default.UploadHostName, url); } #endregion upLoadFileResult.success = true; return(upLoadFileResult); }