/// <summary> /// 上传商品图片 /// </summary> /// <param name="file">图片文件</param> /// <param name="errMsg">异常信息</param> /// <param name="savePath">上传后的相对路径</param> /// <returns>是否上传成功</returns> public static bool UploadGoodsImage(HttpPostedFileBase file, out string savePath, out string errMsg) { errMsg = string.Empty; savePath = string.Empty; if (file == null) { errMsg = "上传的图片不能为空"; return(false); } if (file.ContentLength == 0) { errMsg = "上传的图片不能为空"; return(false); } if (file.ContentLength / 1024 > GoodsImageMaxLength) { errMsg = "上传的图片太大了"; return(false); } if (!ImageExtensions.Contains(Utils.GetUrlSuffix(file.FileName).ToLower())) { errMsg = "上传的图片格式不正确"; return(false); } try { var image = Image.FromStream(file.InputStream, true, true); var now = DateTime.Now; var path = GetGoodsImageFilePath(now, file.FileName); var bigSize = BigGoodsImageSize.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries) .Select(c => Convert.ToInt32(c)).ToList(); var standardSzie = StandardGoodsImageSize.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries) .Select(c => Convert.ToInt32(c)).ToList(); var smallSize = SmallGoodsImageSize.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries) .Select(c => Convert.ToInt32(c)).ToList(); ImageUtility.CutForCustom(file, HttpContext.Current.Server.MapPath(path), image.Width, image.Height, string.Empty, string.Empty); ImageUtility.CutForCustom(file, HttpContext.Current.Server.MapPath(GetGoodsImageFilePath(now, file.FileName, GoodsImageSize.Big)), bigSize[0], bigSize[1], string.Empty, string.Empty); ImageUtility.CutForCustom(file, HttpContext.Current.Server.MapPath(GetGoodsImageFilePath(now, file.FileName, GoodsImageSize.Standard)), standardSzie[0], standardSzie[1], string.Empty, string.Empty); ImageUtility.CutForCustom(file, HttpContext.Current.Server.MapPath(GetGoodsImageFilePath(now, file.FileName, GoodsImageSize.Small)), smallSize[0], smallSize[1], string.Empty, string.Empty); savePath = path; return(true); } catch (Exception) { errMsg = "上传的图片格式不正确"; return(false); } }