增加功能:个人中心头像增加删除确认操作,以及删除自定义头像恢复默认头像功能

This commit is contained in:
Argo-MacBookPro 2018-09-28 15:08:01 +08:00
parent b677927047
commit 46684f3b82
4 changed files with 70 additions and 6 deletions

View File

@ -21,12 +21,13 @@ namespace Bootstrap.Admin.Controllers.Api
long fileSize = 0;
var userName = User.Identity.Name;
var error = string.Empty;
var fileName = string.Empty;
if (User.IsInRole("Administrators")) userName = "default";
if (files.Count > 0)
if (files.Files.Count > 0)
{
var uploadFile = files.Files[0];
var webSiteUrl = DictHelper.RetrieveIconFolderPath().Code;
var fileName = string.Format("{0}{1}", userName, Path.GetExtension(uploadFile.FileName));
fileName = string.Format("{0}{1}", userName, Path.GetExtension(uploadFile.FileName));
var fileUrl = string.Format("{0}{1}", webSiteUrl, fileName);
var filePath = Path.Combine(env.WebRootPath, webSiteUrl.Replace("~", string.Empty).Replace("/", "\\").TrimStart('\\') + fileName);
var fileFolder = Path.GetDirectoryName(filePath);
@ -39,12 +40,31 @@ namespace Bootstrap.Admin.Controllers.Api
previewUrl = string.Format("{0}?q={1}", Url.Content(fileUrl), DateTime.Now.Ticks);
UserHelper.SaveUserIconByName(userName, fileName);
}
else
{
// delete file
fileName = files["key"];
if (!fileName.Equals("default.jpg"))
{
fileName = Path.Combine(env.WebRootPath, $"images\\uploader\\{fileName}");
try
{
System.IO.File.Delete(fileName);
fileName = "default.jpg";
UserHelper.SaveUserIconByName(userName, fileName);
}
catch (Exception ex)
{
error = ex.Message;
}
}
}
return new JsonResult(new
{
error = string.IsNullOrEmpty(error) ? error : $"服务器端错误-{error}",
initialPreview = new string[] { previewUrl },
initialPreviewConfig = new object[] {
new { caption= "新头像", size= fileSize, showZoom= true }
new { caption = "新头像", size = fileSize, showZoom = true, key = fileName }
},
append = false
});

View File

@ -16,6 +16,10 @@ namespace Bootstrap.Admin.Models
/// <summary>
///
/// </summary>
public string FileName { get; }
/// <summary>
///
/// </summary>
/// <param name="controller"></param>
public ProfilesModel(ControllerBase controller) : base(controller)
{
@ -25,6 +29,7 @@ namespace Bootstrap.Admin.Models
if (File.Exists(fileName))
{
Size = new FileInfo(fileName).Length;
FileName = Path.GetFileName(fileName);
}
}
}

View File

@ -99,7 +99,7 @@
<div class="card-body">
<form enctype="multipart/form-data" class="d-none" data-admin="@User.IsInRole("Administrators")">
<div class="form-group">
<input id="fileIcon" class="file" type="file" data-init="@Model.Size">
<input id="fileIcon" class="file" type="file" data-init="@Model.Size" data-file="@Model.FileName">
</div>
</form>
<img class="card-img d-none" src="@Url.Content(Model.Icon)" />

View File

@ -1,8 +1,11 @@
$(function () {
var $headerIcon = $('#headerIcon');
var preIcon = $headerIcon.attr('src');
$('#fileIcon').fileinput({
var $file = $('#fileIcon');
var defFileName = $file.attr('data-file');
$file.fileinput({
uploadUrl: $.formatUrl(Profiles.url),
deleteUrl: $.formatUrl(Profiles.url),
browseOnZoneClick: true,
theme: 'fa',
language: 'zh',
@ -12,7 +15,7 @@
preIcon
],
initialPreviewConfig: [
{ caption: "现在头像", size: $('#fileIcon').attr('data-init'), showZoom: true }
{ caption: "现在头像", size: $file.attr('data-init'), showZoom: true, showRemove: defFileName !== 'default.jpg', key: defFileName }
],
initialPreviewAsData: true,
overwriteInitial: true,
@ -20,8 +23,44 @@
}).on('fileuploaded', function (event, data, previewId, index) {
var url = data.response.initialPreview[0];
if (!!url === true) $headerIcon.attr('src', url);
}).on('filebeforedelete', function (e, file) {
if (file === "default.jpg") {
swal({ showConfirmButton: false, showCancelButton: false, timer: 1500, title: '默认头像不能删除', type: "info" });
return true;
}
return new Promise(function (resolve, reject) {
swal({
title: "您确定要删除吗?",
type: "warning",
showCancelButton: true,
closeOnConfirm: true,
confirmButtonText: "是的,我要删除",
confirmButtonColor: "#d9534f",
cancelButtonText: "取消"
}, function (del) {
resolve(!del);
if (del) $file.fileinput('default');
});
});
}).on('filedeleted', function (event, key, jqXHR, data) {
$headerIcon.attr('src', $.formatUrl('images/uploader/default.jpg'));
});
$.fn.fileinput.Constructor.prototype.default = function () {
$.extend(this, {
initialPreview: [
$.formatUrl('images/uploader/default.jpg')
],
initialPreviewConfig: [
{ caption: "现在头像", size: 7195, showZoom: true, showRemove: false, key: 'default.jpg' }
]
});
this._initPreviewCache();
this._initPreview(true);
this._initPreviewActions();
this._initZoom();
};
$('.site-footer').footer();
var dataBinder = new DataEntity({