From dd22af8c4f19516ac385a714b25484344e19b436 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=8D=E5=81=9A=E7=A0=81=E5=86=9C?= <599854767@qq.com> Date: Tue, 29 Mar 2022 16:45:11 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=A4=B4=E5=83=8F=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../System/SysProfileController.cs | 26 +++++++------------ 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/ZR.Admin.WebApi/Controllers/System/SysProfileController.cs b/ZR.Admin.WebApi/Controllers/System/SysProfileController.cs index 4ec55bf..25f2b6b 100644 --- a/ZR.Admin.WebApi/Controllers/System/SysProfileController.cs +++ b/ZR.Admin.WebApi/Controllers/System/SysProfileController.cs @@ -15,6 +15,8 @@ using ZR.Common; using ZR.Model.System.Dto; using ZR.Model.System; using ZR.Service.System.IService; +using Infrastructure.Extensions; +using System.Threading.Tasks; namespace ZR.Admin.WebApi.Controllers.System { @@ -27,6 +29,7 @@ namespace ZR.Admin.WebApi.Controllers.System private readonly ISysRoleService RoleService; private readonly ISysUserPostService UserPostService; private readonly ISysDeptService DeptService; + private readonly ISysFileService FileService; private OptionsSetting OptionsSetting; private IWebHostEnvironment hostEnvironment; @@ -35,6 +38,7 @@ namespace ZR.Admin.WebApi.Controllers.System ISysRoleService roleService, ISysUserPostService postService, ISysDeptService deptService, + ISysFileService sysFileService, IOptions options, IWebHostEnvironment hostEnvironment) { @@ -42,6 +46,7 @@ namespace ZR.Admin.WebApi.Controllers.System RoleService = roleService; UserPostService = postService; DeptService = deptService; + FileService = sysFileService; OptionsSetting = options.Value; this.hostEnvironment = hostEnvironment; } @@ -124,28 +129,17 @@ namespace ZR.Admin.WebApi.Controllers.System [HttpPost("Avatar")] [ActionPermissionFilter(Permission = "common")] [Log(Title = "修改头像", BusinessType = BusinessType.UPDATE, IsSaveRequestData = false)] - public IActionResult Avatar([FromForm(Name = "picture")] IFormFile formFile) + public async Task Avatar([FromForm(Name = "picture")] IFormFile formFile) { LoginUser loginUser = Framework.JwtUtil.GetLoginUser(HttpContext); - if (formFile == null) throw new CustomException("请选择文件"); string fileExt = Path.GetExtension(formFile.FileName); - string savePath = Path.Combine(hostEnvironment.WebRootPath, FileUtil.GetdirPath("uploads")); + string fileName = FileUtil.HashFileName() + (fileExt.IsEmpty() ? ".png" : fileExt); + SysFile file = await FileService.SaveFileToLocal(hostEnvironment.WebRootPath, fileName, "", HttpContext.GetName(), formFile); - if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } - - string fileName = FileUtil.HashFileName() + fileExt; - string finalFilePath = savePath + fileName; - - using (var stream = new FileStream(finalFilePath, FileMode.Create)) - { - formFile.CopyTo(stream); - } - string accessUrl = $"{OptionsSetting.Upload.UploadUrl}/{FileUtil.GetdirPath("uploads")}{fileName}"; - - UserService.UpdatePhoto(new SysUser() { Avatar = accessUrl, UserId = loginUser.UserId }); - return SUCCESS(new { imgUrl = accessUrl }); + UserService.UpdatePhoto(new SysUser() { Avatar = file.AccessUrl, UserId = loginUser.UserId }); + return SUCCESS(new { imgUrl = file.AccessUrl }); } } }