public ResponsResult Post([FromBody] FileUploadModel model) { ResponsResult result = new ResponsResult(); try { var ext = System.IO.Path.GetExtension(model.FileName); if (string.IsNullOrEmpty(ext)) { ext = ".jpg"; } string _fileName = $"{model.Type}_{DateTime.Now.ToString("yyyyMMddHHmmssfffffff")}{ext}"; var _virtual = PathServerUtility.Combine(System.Enum.GetName(typeof(FileType), model.Type), model.Id.ToString(), _fileName); var webApiPath = ConfigLocator.Instance[TbConstant.WebSiteKey] + "/api/UploadBase64File"; var sign = Security.Sign(Domain.Config.TbConstant.UploadKey, _virtual); model.Picture = Convert.ToBase64String(ImageHandler.ShrinkImage(ImageHandler.Base64ToBytes(model.Picture))); string response; if (!string.IsNullOrEmpty(model.WaterMarks)) { response = HttpUtility.PostString(webApiPath, new { sign = sign, base64 = model.Picture, fileName = _virtual, watermarks = model.WaterMarks }.GetJson(), "application/json"); } else { response = HttpUtility.PostString(webApiPath, new { sign = sign, base64 = model.Picture, fileName = _virtual }.GetJson(), "application/json"); } var uploadModel = (response.GetModel <ResponsResult>().Data as Newtonsoft.Json.Linq.JObject).ToObject <UploadModel>(); result.Data = uploadModel; return(result); } catch (Exception ex) { Log4Net.Error($"[上传图片异常]_:{ex}"); return(result.SetError("上传图片异常")); } }
public ResponsResult UploadBase64File([FromBody] ReqModel model) { ResponsResult result = new ResponsResult(); if (string.IsNullOrWhiteSpace(model.Base64) || string.IsNullOrEmpty(model.FileName)) { var di = PathServerUtility.MapPath(TbConstant.DefaultHeadPicture); FileInfo fi = new FileInfo(di); UploadModel uploadModel = new UploadModel { FileName = fi.Name, FullName = fi.FullName, Extension = fi.Extension, Length = fi.Length, VirtualPath = model.FileName ?? TbConstant.DefaultHeadPicture, FullVirtualPath = PathServerUtility.CombineWithRoot(model.FileName ?? TbConstant.DefaultHeadPicture), }; result.Data = uploadModel; } else { if (!Security.ValidSign(model.Sign, model.FileName, TbConstant.UploadKey)) { return(result.SetStatus(ErrorCode.InvalidSign)); } var filePath = PathServerUtility.MapPath(model.FileName); FileInfo fi = new FileInfo(filePath); var bytes = ImageHandler.Base64ToBytes(model.Base64); if (!fi.Directory.Exists) { fi.Directory.Create(); if (!string.IsNullOrEmpty(model.Watermarks)) { MemoryStream ms = new MemoryStream(bytes); using (Image image = Image.FromStream(ms)) { using (Bitmap bitmap = new Bitmap(image.Width, image.Height)) { using (Graphics graphics = Graphics.FromImage(bitmap)) { graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.CompositingQuality = CompositingQuality.HighQuality; graphics.Clear(Color.Transparent); graphics.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); StringFormat strFormat = new StringFormat(); strFormat.Alignment = StringAlignment.Center; graphics.DrawString(model.Watermarks, new Font("华文彩云", 16, FontStyle.Italic | FontStyle.Bold) , Brushes.Sienna, new PointF(image.Width / 2, image.Height - image.Height / 8), strFormat); bitmap.Save(filePath, ImageFormat.Png); } } } } } else { using (var fs = fi.Create()) { fs.Write(bytes, 0, bytes.Length); fs.Flush(true); fi.Refresh(); } } var webSite = ConfigLocator.Instance[TbConstant.WebSiteKey]; UploadModel uploadModel = new UploadModel { FileName = fi.Name, FullName = fi.FullName, Extension = fi.Extension, Length = fi.Length, VirtualPath = model.FileName, FullVirtualPath = PathServerUtility.CombineWithRoot(webSite, model.FileName), }; result.Data = uploadModel; } return(result); }