//****************************** 图片处理Strat ****************************** public static ProductGroupInfoEntity GetProductGroupInfoByCommonSku(string commonSku, string companyCode) { DataCommand cmd = DataCommandManager.GetDataCommand("GetProductGroupInfoByCommonSku"); cmd.SetParameterValue("@CommonSku", commonSku); cmd.SetParameterValue("@CompanyCode", companyCode); ProductGroupInfoEntity result = cmd.ExecuteEntity <ProductGroupInfoEntity>(); return(result); }
/// <summary> /// 上传文件 /// </summary> /// <param name="productGroupInfoImage"></param> /// <returns></returns> public ProductGroupInfoMediaEntity UploadFile(ProductGroupInfoMediaEntity productGroupInfoMedia, string commonSKUNumber) { //文件后缀 string fileFix = ""; FileStream fs = null; try { //判断上传文件是否为第一次上传 if (productGroupInfoMedia.IsFirstChunck) { //取得文件名中CommonInfoSkuNumber //string commonSKUNumber = ""; //var re = new Regex(@"^([^\._]+)(_(([0-9]+)|(0[1-9]?)))?\..+?$", RegexOptions.IgnoreCase); //var re = new Regex(@"(\w+[/])+\w{2,5}(-\w{2,5})+(_\w{2,5})*\.jpg", RegexOptions.IgnoreCase); //var match = re.Match(productGroupInfoMedia.FileName); //Check上传文件名是否合法 //if (match.Success) //{ // commonSKUNumber = match.Groups[1].Value; //} //else //{ // throw new BusinessException("上传文件名不合法,请检查上传文件名称。"); //} //根据取得商品组信息 ProductGroupInfoEntity groupInfo = SellerPortalProductDescAndImageDA.GetProductGroupInfoByCommonSku(commonSKUNumber, productGroupInfoMedia.Header.CompanyCode); if (groupInfo == null) { throw new BusinessException("上传文件名不属于任何商品组,请检查上传文件名称。"); } //判断ProductGroupSysno如果大于0,则需要Check文件名是否属于该组 if (productGroupInfoMedia.ProductGroupSysno > 0) { //Check文件名是否属于该组 if (productGroupInfoMedia.ProductGroupSysno != groupInfo.SysNo) { throw new BusinessException("上传文件不属于该组。"); } } productGroupInfoMedia.ProductGroupSysno = groupInfo.SysNo; //如果为第一段文件需要判断文件类型是否合法 fileFix = GetFileFix(productGroupInfoMedia.ChunckData); //合法创建文件,否则抛出异常 if (!fileFix.Equals(GetFileFixByType(productGroupInfoMedia.FileType))) { throw new BusinessException("上传文件类型不合法,请检查上传文件类型。"); } //判断文件夹是否存在 if (!Directory.Exists(TemporaryPath)) { Directory.CreateDirectory(TemporaryPath); } //创建临时文件 fs = File.Create(TemporaryPath + productGroupInfoMedia.FileGuid.ToString()); } else { //读取临时文件 fs = File.Open(TemporaryPath + productGroupInfoMedia.FileGuid.ToString(), FileMode.Append); } //循环将数据流写入临时文件进行保存 foreach (byte chunck in productGroupInfoMedia.ChunckData) { fs.WriteByte(chunck); } fs.Close(); fs.Dispose(); productGroupInfoMedia.ChunckData = null; return(productGroupInfoMedia); } catch (Exception ex) { throw new BusinessException(ex.Message); } finally { if (fs != null) { fs.Close(); fs.Dispose(); } } }