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 @@