/// <summary> /// 保存图片 /// </summary> /// <param name="env"></param> /// <param name="file"></param> /// <param name="req"></param> /// <param name="saveSuffix"></param> /// <returns></returns> public static async Task <string> SaveImage(IHostingEnvironment env, IFormFile file, HttpRequest req, string saveSuffix) { var filePath = string.Format("/Uploads/Images/{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); var qiniuFilePath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); string imageUrl = string.Empty; if (!Directory.Exists(env.WebRootPath + filePath)) { Directory.CreateDirectory(env.WebRootPath + filePath); } var strDateTime = DateTime.Now.ToString("yyyyMMddhhmmssfff"); var strRan = Convert.ToString(new Random().Next(100, 999)); var saveName = strDateTime + strRan; var useQiNiu = BuilderContext.Configuration["UseQiNiu"]; var qiniuHost = BuilderContext.Configuration["QiNiuHost"]; var pathName = string.Empty; string hostPath = WebSiteUtility.MapPhysicsToUrl($"{filePath}{saveName}"); string qiniuHostPath = WebSiteUtility.MapPhysicsToUrl($"{qiniuHost}{qiniuFilePath}{saveName}"); using (var stream = new FileStream($"{env.WebRootPath}{filePath}{saveName}{saveSuffix}", FileMode.Create)) { await file.CopyToAsync(stream); stream.Flush(); pathName = stream.Name; imageUrl = $"{req.Scheme}://{req.Host}{hostPath}{saveSuffix}"; } if (useQiNiu.Equals("true", StringComparison.CurrentCultureIgnoreCase)) { await Task.Run(() => { QiniuManager qiniu = new QiniuManager(); string savekey = $"{qiniuFilePath}{saveName}"; byte[] data = System.IO.File.ReadAllBytes(pathName); qiniu.ByteUpload(data, savekey); }); imageUrl = qiniuHostPath; } return(imageUrl.IsNotNullAndWhiteSpace() ? imageUrl : null); }
/// <summary> /// 获取Logo的URL /// </summary> /// <returns></returns> public string LogoUrl(string logoUrl, string commonTypeKey, string imageSizeTypeKey, bool enableClientCaching = true) { if (string.IsNullOrEmpty(logoUrl)) { return(WebSiteUtility.ResolveUrl("~/wwwroot/images/potu.jpg")); } if (!logoUrl.ToLower().EndsWith(".gif") && !string.IsNullOrEmpty(imageSizeTypeKey) && imageSizeTypeKey != ImageSizeTypeKey.Instance().Original()) { LogoSettings logoSettings = LogoSettings.GetRegisteredSettings(commonTypeKey); if (logoSettings != null && logoSettings.ImageSizeTypeKey != null && logoSettings.ImageSizeTypeKey.ContainsKey(imageSizeTypeKey)) { var imageSizeType = logoSettings.ImageSizeTypeKey[imageSizeTypeKey]; string sizeImageName = storeProvider.GetSizeImageName(logoUrl, imageSizeType.Key, imageSizeType.Value); if (enableClientCaching) { var filePath = storeProvider.GetDirectlyUrl(sizeImageName); return(filePath); } else { return(storeProvider.GetDirectlyUrl(sizeImageName, DateTime.Now)); } } } if (enableClientCaching) { return(storeProvider.GetDirectlyUrl(logoUrl)); } else { return(storeProvider.GetDirectlyUrl(logoUrl, DateTime.Now)); } }
public async Task <IActionResult> SaveHeadImage([FromServices] IHostingEnvironment env, IFormFile files, long userId) { var file = Request.Form.Files[0]; long size = Request.Form.Files.Sum(f => f.Length); var x1 = Request.GetInt("x1", 0); var y1 = Request.GetInt("y1", 0); var w = Request.GetInt("w", 0); var h = Request.GetInt("h", 0); var filePath = string.Format("/Uploads/Images/{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); var qiniuFilePath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); if (!Directory.Exists(env.WebRootPath + filePath)) { Directory.CreateDirectory(env.WebRootPath + filePath); } if (file != null) { var fileExtension = Path.GetExtension(file.FileName); const string fileFilt = ".gif|.jpg|.jpeg|.png|.webp|"; const string saveSuffix = ".jpeg"; if (fileExtension == null) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件没有后缀"))); } if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不是图片"))); } if (size > 1024 * 1024 * 2) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不能大于2M"))); } var strDateTime = DateTime.Now.ToString("yyyyMMddhhmmssfff"); var strRan = Convert.ToString(new Random().Next(100, 999)); var saveName = strDateTime + strRan; var useQiNiu = BuilderContext.Configuration["UseQiNiu"]; var qiniuHost = BuilderContext.Configuration["QiNiuHost"]; string hostPath = WebSiteUtility.MapPhysicsToUrl($"{filePath}{saveName}"); string qiniuHostPath = WebSiteUtility.MapPhysicsToUrl($"{qiniuHost}{qiniuFilePath}{saveName}"); await Task.Factory.StartNew(() => { byte[] cutBytes = ConvertUtility.StreamToBytes(file.OpenReadStream()); Bitmap bitmap = ConvertUtility.BytesToBitmap(cutBytes); var scale = bitmap.Height / 360.00; Bitmap newBitMap = Utility.CutImage(bitmap, Convert.ToInt32(x1 *scale), Convert.ToInt32(y1 *scale), Convert.ToInt32(w *scale), Convert.ToInt32(h *scale)); Bitmap newBitMap100 = Utility.ResizeImage(newBitMap, 100, 100); Bitmap newBitMap50 = Utility.ResizeImage(newBitMap, 50, 50); Bitmap newBitMap30 = Utility.ResizeImage(newBitMap, 30, 30); newBitMap.Save($"{env.WebRootPath}{filePath}{saveName}.jpeg", ImageFormat.Jpeg); newBitMap100.Save($"{env.WebRootPath}{filePath}{saveName}_100x100{saveSuffix}", ImageFormat.Jpeg); newBitMap50.Save($"{env.WebRootPath}{filePath}{saveName}_50x50{saveSuffix}", ImageFormat.Jpeg); newBitMap30.Save($"{env.WebRootPath}{filePath}{saveName}_30x30{saveSuffix}", ImageFormat.Jpeg); }); if (useQiNiu.Equals("true", StringComparison.CurrentCultureIgnoreCase)) { await Task.Run(() => { QiniuManager qiniu = new QiniuManager(); string savekey = $"{qiniuFilePath}{saveName}"; byte[] data = System.IO.File.ReadAllBytes($"{env.WebRootPath}{filePath}{saveName}{saveSuffix}"); qiniu.ByteUpload(data, savekey); }); await Task.Run(() => { QiniuManager qiniu = new QiniuManager(); string savekey = $"{qiniuFilePath}{saveName}_100x100"; byte[] data = System.IO.File.ReadAllBytes($"{env.WebRootPath}{filePath}{saveName}_100x100{saveSuffix}"); qiniu.ByteUpload(data, savekey); }); await Task.Run(() => { QiniuManager qiniu = new QiniuManager(); string savekey = $"{qiniuFilePath}{saveName}_50x50"; byte[] data = System.IO.File.ReadAllBytes($"{env.WebRootPath}{filePath}{saveName}_50x50{saveSuffix}"); qiniu.ByteUpload(data, savekey); }); await Task.Run(() => { QiniuManager qiniu = new QiniuManager(); string savekey = $"{qiniuFilePath}{saveName}_30x30"; byte[] data = System.IO.File.ReadAllBytes($"{env.WebRootPath}{filePath}{saveName}_30x30{saveSuffix}"); qiniu.ByteUpload(data, savekey); }); } User user = userService.GetFullUser(userId); if (useQiNiu.Equals("true", StringComparison.CurrentCultureIgnoreCase)) { user = membershipService.SetHeadImage(user, $"{qiniuHost}{qiniuFilePath}{saveName}"); } else { user = membershipService.SetHeadImage(user, $"{Request.Scheme}://{Request.Host}{filePath}{saveName}{saveSuffix}"); } if (user != null) { HttpContext.Session.Set <User>(user.UserId.ToString(), user); } return(Json(new StatusMessageData(StatusMessageType.Success, "上传成功"))); } return(Json(new StatusMessageData(StatusMessageType.Error, "上传失败"))); }
public async Task <IActionResult> HomeDynamicAddUpdateAsync([FromServices] IHostingEnvironment env, IFormFile files, HomeDynamicModel model) { ValidateInput(model, ModelState); if (model.Id == 0) { model.IsUpdate = false; } else { model.IsUpdate = true; } string message = string.Empty; if (ModelState.IsValid) { if (model.ImageUrl.IsNullOrWhiteSpace()) { var file = Request.Form.Files[0]; long size = Request.Form.Files.Sum(f => f.Length); var filePath = string.Format("/Uploads/Images/{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); var qiniuFilePath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); if (!Directory.Exists(env.WebRootPath + filePath)) { Directory.CreateDirectory(env.WebRootPath + filePath); } if (file != null) { var fileExtension = Path.GetExtension(file.FileName); const string fileFilt = ".gif|.jpg|.jpeg|.png|.webp|"; const string saveSuffix = ".jpeg"; if (fileExtension == null) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件没有后缀"))); } if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不是图片"))); } if (size > 1024 * 1024 * 2) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不能大于2M"))); } var strDateTime = DateTime.Now.ToString("yyyyMMddhhmmssfff"); var strRan = Convert.ToString(new Random().Next(100, 999)); var saveName = strDateTime + strRan; var useQiNiu = BuilderContext.Configuration["UseQiNiu"]; var qiniuHost = BuilderContext.Configuration["QiNiuHost"]; var pathName = string.Empty; string hostPath = WebSiteUtility.MapPhysicsToUrl($"{filePath}{saveName}"); string qiniuHostPath = WebSiteUtility.MapPhysicsToUrl($"{qiniuHost}{qiniuFilePath}{saveName}"); using (var stream = new FileStream($"{env.WebRootPath}{filePath}{saveName}{saveSuffix}", FileMode.Create)) { await file.CopyToAsync(stream); stream.Flush(); pathName = stream.Name; model.ImageUrl = $"{Request.Scheme}://{Request.Host}{hostPath}{saveSuffix}"; } if (useQiNiu.Equals("true", StringComparison.CurrentCultureIgnoreCase)) { await Task.Run(() => { QiniuManager qiniu = new QiniuManager(); string savekey = $"{qiniuFilePath}{saveName}"; byte[] data = System.IO.File.ReadAllBytes(pathName); qiniu.ByteUpload(data, savekey); }); model.ImageUrl = qiniuHostPath; } } } if (model.IsUpdate) { HomeDynamicSettings dynamic = homeDynamicSettingsService.GetHomeDynamicSingle(model.Id); dynamic.DateLastModified = DateTime.Now; dynamic.ImageUrl = model.ImageUrl; dynamic.LabelText = model.LabelText; dynamic.RedirectUrl = model.RedirectUrl; homeDynamicSettingsService.UpdateHomeDynamic(dynamic, out message); } else { HomeDynamicSettings dynamic = model.AsHomeDynamic(); dynamic.Userid = UserContext.CurrentUser.UserId; dynamic.DisplayOrder = homeDynamicSettingsService.GetMaxDisplayOrder(model.Type); homeDynamicSettingsService.AddHomeDynamic(dynamic, out message); } return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功"))); } return(Json(new StatusMessageData(StatusMessageType.Error, "保存失败"))); }
/// <summary> /// 获取所有标题图配置 /// </summary> /// <returns></returns> public XElement GetAllLogoConfigs() { XElement xelement = XElement.Load(WebSiteUtility.GetPhysicalFilePath(configPath)); return(xelement); }
public async Task <IActionResult> EditCategoryLevelAsync([FromServices] IHostingEnvironment env, IFormFile files, CategoryLevelModel model, bool isUpdate) { var file = files; if (file != null) { var filePath = string.Format("/Uploads/Images/{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); var qiniuFilePath = string.Format("{0}/{1}/{2}/", DateTime.Now.ToString("yyyy"), DateTime.Now.ToString("MM"), DateTime.Now.ToString("dd")); if (!Directory.Exists(env.WebRootPath + filePath)) { Directory.CreateDirectory(env.WebRootPath + filePath); } long size = file.Length; var fileExtension = Path.GetExtension(file.FileName); const string fileFilt = ".gif|.jpg|.jpeg|.png|.webp|"; const string saveSuffix = ".jpeg"; if (fileExtension == null) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件没有后缀"))); } if (fileFilt.IndexOf(fileExtension.ToLower(), StringComparison.Ordinal) <= -1) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不是图片"))); } if (size > 1024 * 1024 * 2) { return(Json(new StatusMessageData(StatusMessageType.Error, "上传的文件不能大于2M"))); } var strDateTime = DateTime.Now.ToString("yyyyMMddhhmmssfff"); var strRan = Convert.ToString(new Random().Next(100, 999)); var saveName = strDateTime + strRan; var useQiNiu = BuilderContext.Configuration["UseQiNiu"]; var qiniuHost = BuilderContext.Configuration["QiNiuHost"]; var pathName = string.Empty; string hostPath = WebSiteUtility.MapPhysicsToUrl($"{filePath}{saveName}"); string qiniuHostPath = WebSiteUtility.MapPhysicsToUrl($"{qiniuHost}{qiniuFilePath}{saveName}"); using (var stream = new FileStream($"{env.WebRootPath}{filePath}{saveName}{saveSuffix}", FileMode.Create)) { await file.CopyToAsync(stream); stream.Flush(); pathName = stream.Name; model.ImageUrl = $"{Request.Scheme}://{Request.Host}{hostPath}{saveSuffix}"; } if (useQiNiu.Equals("true", StringComparison.CurrentCultureIgnoreCase)) { await Task.Run(() => { QiniuManager qiniu = new QiniuManager(); string savekey = $"{qiniuFilePath}{saveName}"; byte[] data = System.IO.File.ReadAllBytes(pathName); qiniu.ByteUpload(data, savekey); }); model.ImageUrl = qiniuHostPath; } } if (isUpdate) { if (model.LevelEnum == CategoryLevel.Level1) { GoodsCategoryLevel1 category = goodsCategoryLevelService.GetCategoryLevel1(model.CategoryId); category.CategoryName = model.CategoryName; category.Description = model.Description ?? string.Empty; category.CategoryLevel1ImageUrl = model.ImageUrl ?? string.Empty; goodsCategoryLevelService.UpdataCategoryLevel <GoodsCategoryLevel1>(category); return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功"))); } else if (model.LevelEnum == CategoryLevel.Level2) { GoodsCategoryLevel2 category = goodsCategoryLevelService.GetCategoryLevel2(model.CategoryId); var oldCategoryLevel2Id = category.CategoryLevel1Id; category.CategoryName = model.CategoryName; category.CategoryLevel1Id = model.ParentCategoryId.Value; category.Description = model.Description ?? string.Empty; category.CategoryLevel2ImageUrl = model.ImageUrl ?? string.Empty; goodsCategoryLevelService.UpdataCategoryLevel <GoodsCategoryLevel2>(category, oldCategoryLevel2Id); return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功"))); } } else { if (model.LevelEnum == CategoryLevel.Level1) { GoodsCategoryLevel1 category = GoodsCategoryLevel1.New(); category.CategoryName = model.CategoryName; category.Description = model.Description ?? string.Empty; category.CategoryLevel1ImageUrl = model.ImageUrl ?? string.Empty; goodsCategoryLevelService.CreateCategoryLevel <GoodsCategoryLevel1>(category); return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功"))); } else if (model.LevelEnum == CategoryLevel.Level2) { GoodsCategoryLevel2 category = GoodsCategoryLevel2.New(); category.CategoryName = model.CategoryName; category.CategoryLevel1Id = model.ParentCategoryId.Value; category.Description = model.Description ?? string.Empty; category.CategoryLevel2ImageUrl = model.ImageUrl ?? string.Empty; goodsCategoryLevelService.CreateCategoryLevel <GoodsCategoryLevel2>(category); return(Json(new StatusMessageData(StatusMessageType.Success, "保存成功"))); } } return(Json(new StatusMessageData(StatusMessageType.Error, "保存失败"))); }