public JsonResult Upload() { HttpPostedFileBase file = Request.Files[0]; if (file != null) { if (file.ContentLength > 2097152) { return(Json(new ResultModel(false, "图片太大"), JsonRequestBehavior.AllowGet)); } string fileExt = Path.GetExtension(file.FileName).ToLower(); if (string.IsNullOrEmpty(fileExt)) { return(Json(new ResultModel(false, "格式不对"), JsonRequestBehavior.AllowGet)); } else { bool allowUploadExt = false; string allUploadExts = ".gif|.jpg|.jpeg|.png"; string[] exps = allUploadExts.Split('|'); foreach (string exp in exps) { if (exp.ToLower() == fileExt) { allowUploadExt = true; break; } } if (!allowUploadExt) { return(Json(new ResultModel(false, "请上传正确的格式"), JsonRequestBehavior.AllowGet)); } } var fileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyyMMddhhmmss"), file.FileName); if (!System.IO.Directory.Exists(Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.PhysicalPath)) { System.IO.Directory.CreateDirectory(Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.PhysicalPath); } var fullFilePath = Path.Combine(Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.PhysicalPath, fileName); file.SaveAs(fullFilePath); var transformer = new ETS.Compress.FixedDimensionTransformerAttribute(CustomerIconUploader.Instance.Width, CustomerIconUploader.Instance.Height, CustomerIconUploader.Instance.MaxBytesLength / 1024); var destFileName = string.Format("{0}_{1}{2}", DateTime.Now.ToString("yyyyMMddhhmmss"), new Random().Next(1000), Path.GetExtension(file.FileName)); var destFullFileName = System.IO.Path.Combine(CustomerIconUploader.Instance.PhysicalPath, destFileName); transformer.Transform(fullFilePath, destFullFileName); var picUrl = System.IO.Path.GetFileName(destFullFileName); return(Json(new ResultModel(true, picUrl + "|" + file.FileName), JsonRequestBehavior.AllowGet)); } return(Json(new ResultModel(false, ""), JsonRequestBehavior.AllowGet)); }
public ImgInfo UploadImg(HttpPostedFile httpPostedFile, int orderId) { ImgInfo imgInfo = new ImgInfo(); try { System.Drawing.Image img = System.Drawing.Image.FromStream(httpPostedFile.InputStream); } catch (Exception) { imgInfo.FailRemark = "无图片流"; return(imgInfo); } var fileName = ETS.Util.ImageTools.GetFileName(Path.GetExtension(httpPostedFile.FileName)); imgInfo.FileName = fileName; int fileNameLastDot = fileName.LastIndexOf('.'); //原图 string rFileName = string.Format("{0}{1}{2}", fileName.Substring(0, fileNameLastDot), SystemConst.OriginSize, Path.GetExtension(fileName)); //原始的 imgInfo.OriginFileName = rFileName; string saveDbFilePath; string saveDir = ""; if (orderId > 0) { saveDir = orderId.ToString(); } string fullFileDir = ETS.Util.ImageTools.CreateDirectory(Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.PhysicalPath, saveDir, out saveDbFilePath); imgInfo.FullFileDir = fullFileDir; imgInfo.SaveDbFilePath = saveDbFilePath; if (fullFileDir == "0") { imgInfo.FailRemark = "创建目录失败"; return(imgInfo); } //保存原图, ///TODO记录图片大小 var fullFilePath = Path.Combine(fullFileDir, rFileName); httpPostedFile.SaveAs(fullFilePath); //裁图 var transformer = new ETS.Compress.FixedDimensionTransformerAttribute(Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.Width, Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.Height, Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.MaxBytesLength / 1024); //保存到数据库的图片路径 var destFullFileName = System.IO.Path.Combine(fullFileDir, fileName); transformer.Transform(fullFilePath, destFullFileName); var picUrl = saveDbFilePath + fileName; imgInfo.PicUrl = picUrl; return(imgInfo); }
/// <summary> /// 上传图片 修改:彭宜 20150710 /// </summary> /// <param name="httpPostedFile">待传输文件</param> /// <param name="orderId"></param> /// <param name="imageType">图片类型</param> /// <returns></returns> public ImgInfo UploadImg(HttpPostedFile httpPostedFile, UploadFrom uploadFrom) { ImgInfo imgInfo = new ImgInfo(); try { System.Drawing.Image img = System.Drawing.Image.FromStream(httpPostedFile.InputStream); } catch (Exception) { imgInfo.FailRemark = "无图片流"; return imgInfo; } var fileName = ETS.Util.ImageTools.GetFileName(Path.GetExtension(httpPostedFile.FileName)); imgInfo.FileName = fileName; imgInfo.OriginalName = httpPostedFile.FileName; int fileNameLastDot = fileName.LastIndexOf('.'); //原图 string rFileName = string.Format("{0}{1}{2}", fileName.Substring(0, fileNameLastDot), SystemConst.OriginSize, Path.GetExtension(fileName)); //原始的 imgInfo.OriginFileName = rFileName; string saveDbFilePath; string saveDir = ""; string basePath = Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.GetPhysicalPath(uploadFrom); string fullFileDir = ETS.Util.ImageTools.CreateDirectory(basePath, "", out saveDbFilePath); imgInfo.FullFileDir = fullFileDir; imgInfo.SaveDbFilePath = saveDbFilePath; if (fullFileDir == "0") { imgInfo.FailRemark = "创建目录失败"; return imgInfo; } //保存原图, ///TODO记录图片大小 var fullFilePath = Path.Combine(fullFileDir, rFileName); httpPostedFile.SaveAs(fullFilePath); //裁图 var transformer = new ETS.Compress.FixedDimensionTransformerAttribute(Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.Width, Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.Height, Ets.Model.ParameterModel.Clienter.CustomerIconUploader.Instance.MaxBytesLength / 1024); //保存到数据库的图片路径 var destFullFileName = System.IO.Path.Combine(fullFileDir, fileName); transformer.Transform(fullFilePath, destFullFileName); var picUrl = saveDbFilePath + fileName; imgInfo.RelativePath = EnumUtils.GetEnumDescription(uploadFrom) + picUrl; imgInfo.PicUrl = ImageCommon.ReceiptPicConvert(uploadFrom, picUrl); return imgInfo; }