fix(#IW2JB): 个人中心上传新头像前台系统不能更新

#Comment
重新设计头像保存机制,数据库保存头像文件名加时间戳如 admin.jpg?v=543891,前台请求时自动破坏缓存进行更新显示最新头像
comment #IW2JB

#Issue
close https://gitee.com/LongbowEnterprise/dashboard/issues?id=IW2JB
This commit is contained in:
Argo Zhang 2019-04-29 23:58:38 +08:00
parent c7c46d7438
commit 93def71fab
4 changed files with 13 additions and 10 deletions

View File

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

View File

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

View File

@ -88,7 +88,7 @@
</div>
<div class="dropdown userinfo">
<a data-toggle="dropdown" class="dropdown-toggle shadow-default" href="#">
<img id="headerIcon" alt="" src="@Url.Content(Model.Icon)" asp-append-version="true">
<img id="headerIcon" alt="" src="@Url.Content(Model.Icon)">
<span id="userDisplayName" data-userName="@Model.UserName" class="username text-truncate d-inline-block">@Model.DisplayName</span>
</a>
<div class="dropdown-menu dropdown-menu-right">

View File

@ -238,7 +238,7 @@ namespace Bootstrap.DataAccess
public static bool SaveUserIconByName(string userName, string iconName)
{
var ret = DbContextManager.Create<User>().SaveUserIconByName(userName, iconName);
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersDataKey}*");
if (ret) CacheCleanUtility.ClearCache(cacheKey: $"{RetrieveUsersByNameDataKey}*");
return ret;
}