From 93def71fabb6e3fd4dfbb08f755040e527719623 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Mon, 29 Apr 2019 23:58:38 +0800 Subject: [PATCH] =?UTF-8?q?fix(#IW2JB):=20=E4=B8=AA=E4=BA=BA=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E4=B8=8A=E4=BC=A0=E6=96=B0=E5=A4=B4=E5=83=8F=E5=89=8D?= =?UTF-8?q?=E5=8F=B0=E7=B3=BB=E7=BB=9F=E4=B8=8D=E8=83=BD=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #Comment 重新设计头像保存机制,数据库保存头像文件名加时间戳如 admin.jpg?v=543891,前台请求时自动破坏缓存进行更新显示最新头像 comment #IW2JB #Issue close https://gitee.com/LongbowEnterprise/dashboard/issues?id=IW2JB --- .../Controllers/Api/ProfilesController.cs | 15 +++++++-------- Bootstrap.Admin/Models/ProfilesModel.cs | 4 ++++ Bootstrap.Admin/Views/Shared/Header.cshtml | 2 +- Bootstrap.DataAccess/Helper/UserHelper.cs | 2 +- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/ProfilesController.cs b/Bootstrap.Admin/Controllers/Api/ProfilesController.cs index 07a630a4..0775092a 100644 --- a/Bootstrap.Admin/Controllers/Api/ProfilesController.cs +++ b/Bootstrap.Admin/Controllers/Api/ProfilesController.cs @@ -40,11 +40,11 @@ namespace Bootstrap.Admin.Controllers.Api if (System.IO.File.Exists(fileName)) System.IO.File.Delete(fileName); fileName = "default.jpg"; var webSiteUrl = DictHelper.RetrieveIconFolderPath(); - var fileUrl = string.Format("{0}{1}", webSiteUrl, fileName); var filePath = Path.Combine(env.WebRootPath, webSiteUrl.Replace("~", string.Empty).Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar) + fileName); fileSize = new FileInfo(filePath).Length; - previewUrl = string.Format("{0}?q={1}", Url.Content(fileUrl), DateTime.Now.Ticks); - UserHelper.SaveUserIconByName(userName, fileName); + var iconName = $"{fileName}?v={DateTime.Now.Ticks}"; + previewUrl = Url.Content($"{webSiteUrl}{iconName}"); + UserHelper.SaveUserIconByName(userName, iconName); } catch (Exception ex) { @@ -88,13 +88,11 @@ namespace Bootstrap.Admin.Controllers.Api var userName = User.Identity.Name; var error = string.Empty; var fileName = string.Empty; - if (User.IsInRole("Administrators")) userName = "default"; if (files.Files.Count > 0) { var uploadFile = files.Files[0]; var webSiteUrl = DictHelper.RetrieveIconFolderPath(); - fileName = string.Format("{0}{1}", userName, Path.GetExtension(uploadFile.FileName)); - var fileUrl = string.Format("{0}{1}", webSiteUrl, fileName); + fileName = $"{userName}{Path.GetExtension(uploadFile.FileName)}"; var filePath = Path.Combine(env.WebRootPath, webSiteUrl.Replace("~", string.Empty).Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar) + fileName); var fileFolder = Path.GetDirectoryName(filePath); fileSize = uploadFile.Length; @@ -103,8 +101,9 @@ namespace Bootstrap.Admin.Controllers.Api { await uploadFile.CopyToAsync(fs); } - previewUrl = string.Format("{0}?q={1}", Url.Content(fileUrl), DateTime.Now.Ticks); - UserHelper.SaveUserIconByName(userName, fileName); + var iconName = $"{fileName}?v={DateTime.Now.Ticks}"; + previewUrl = Url.Content($"{webSiteUrl}{iconName}"); + UserHelper.SaveUserIconByName(userName, iconName); } return new JsonResult(new { diff --git a/Bootstrap.Admin/Models/ProfilesModel.cs b/Bootstrap.Admin/Models/ProfilesModel.cs index 32951ea7..ff5cd6f0 100644 --- a/Bootstrap.Admin/Models/ProfilesModel.cs +++ b/Bootstrap.Admin/Models/ProfilesModel.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc; using System.IO; +using System.Linq; namespace Bootstrap.Admin.Models { @@ -29,6 +30,9 @@ namespace Bootstrap.Admin.Models if (host != null) { var fileName = Path.Combine(host.WebRootPath, Icon.TrimStart('~', '/').Replace('/', Path.DirectorySeparatorChar)); + + // 数据库存储的个人图片有后缀 default.jpg?v=1234567 + fileName = fileName.Split('?').FirstOrDefault(); if (File.Exists(fileName)) { Size = new FileInfo(fileName).Length; diff --git a/Bootstrap.Admin/Views/Shared/Header.cshtml b/Bootstrap.Admin/Views/Shared/Header.cshtml index 78d82b1d..6eba5e63 100644 --- a/Bootstrap.Admin/Views/Shared/Header.cshtml +++ b/Bootstrap.Admin/Views/Shared/Header.cshtml @@ -88,7 +88,7 @@