2018-06-07 00:45:47 +08:00
|
|
|
|
using Bootstrap.DataAccess;
|
2018-06-24 16:16:58 +08:00
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2018-06-24 16:16:58 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
|
|
|
|
|
namespace Bootstrap.Admin.Controllers.Api
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Route("api/[controller]")]
|
2018-11-24 15:12:44 +08:00
|
|
|
|
[ApiController]
|
|
|
|
|
public class ProfilesController : ControllerBase
|
2018-06-07 00:45:47 +08:00
|
|
|
|
{
|
2019-03-10 17:59:36 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除头像按钮调用
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="id">Delete</param>
|
|
|
|
|
/// <param name="env"></param>
|
|
|
|
|
/// <param name="files">表单数据集合</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
[HttpPost("{id}")]
|
|
|
|
|
public JsonResult Post(string id, [FromServices]IHostingEnvironment env, [FromForm]DeleteFileCollection files)
|
|
|
|
|
{
|
|
|
|
|
if (!id.Equals("Delete", StringComparison.OrdinalIgnoreCase) || files.Key.Equals("default.jpg", StringComparison.OrdinalIgnoreCase)) return new JsonResult(new object());
|
|
|
|
|
|
|
|
|
|
var previewUrl = string.Empty;
|
|
|
|
|
long fileSize = 0;
|
|
|
|
|
var userName = User.Identity.Name;
|
|
|
|
|
var error = string.Empty;
|
|
|
|
|
var fileName = files.Key;
|
|
|
|
|
|
|
|
|
|
fileName = Path.Combine(env.WebRootPath, $"images{Path.DirectorySeparatorChar}uploader{Path.DirectorySeparatorChar}{fileName}");
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
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, key = "default.jpg" }
|
|
|
|
|
},
|
|
|
|
|
append = false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-28 15:08:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
///
|
|
|
|
|
/// </summary>
|
2019-03-10 17:59:36 +08:00
|
|
|
|
public class DeleteFileCollection
|
|
|
|
|
{
|
|
|
|
|
public string Key { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 上传头像按钮调用
|
|
|
|
|
/// </summary>
|
2018-10-28 15:08:58 +08:00
|
|
|
|
/// <param name="env"></param>
|
|
|
|
|
/// <param name="files"></param>
|
|
|
|
|
/// <returns></returns>
|
2018-06-07 00:45:47 +08:00
|
|
|
|
[HttpPost]
|
2018-06-24 16:16:58 +08:00
|
|
|
|
public async Task<JsonResult> Post([FromServices]IHostingEnvironment env, IFormCollection files)
|
2018-06-07 00:45:47 +08:00
|
|
|
|
{
|
2018-06-24 16:16:58 +08:00
|
|
|
|
var previewUrl = string.Empty;
|
|
|
|
|
long fileSize = 0;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
var userName = User.Identity.Name;
|
2018-06-24 16:16:58 +08:00
|
|
|
|
var error = string.Empty;
|
2018-09-28 15:08:01 +08:00
|
|
|
|
var fileName = string.Empty;
|
2018-06-07 00:45:47 +08:00
|
|
|
|
if (User.IsInRole("Administrators")) userName = "default";
|
2018-09-28 15:08:01 +08:00
|
|
|
|
if (files.Files.Count > 0)
|
2018-06-24 16:16:58 +08:00
|
|
|
|
{
|
|
|
|
|
var uploadFile = files.Files[0];
|
2019-01-11 23:20:28 +08:00
|
|
|
|
var webSiteUrl = DictHelper.RetrieveIconFolderPath();
|
2018-09-28 15:08:01 +08:00
|
|
|
|
fileName = string.Format("{0}{1}", userName, Path.GetExtension(uploadFile.FileName));
|
2018-06-24 16:16:58 +08:00
|
|
|
|
var fileUrl = string.Format("{0}{1}", webSiteUrl, fileName);
|
2019-03-09 20:04:04 +08:00
|
|
|
|
var filePath = Path.Combine(env.WebRootPath, webSiteUrl.Replace("~", string.Empty).Replace('/', Path.DirectorySeparatorChar).TrimStart(Path.DirectorySeparatorChar) + fileName);
|
2018-06-24 16:16:58 +08:00
|
|
|
|
var fileFolder = Path.GetDirectoryName(filePath);
|
|
|
|
|
fileSize = uploadFile.Length;
|
2018-09-08 21:23:00 +08:00
|
|
|
|
if (!Directory.Exists(fileFolder)) Directory.CreateDirectory(fileFolder);
|
|
|
|
|
using (var fs = new FileStream(filePath, FileMode.Create))
|
2018-06-24 16:16:58 +08:00
|
|
|
|
{
|
2018-09-08 21:23:00 +08:00
|
|
|
|
await uploadFile.CopyToAsync(fs);
|
2018-06-24 16:16:58 +08:00
|
|
|
|
}
|
2018-09-08 21:23:00 +08:00
|
|
|
|
previewUrl = string.Format("{0}?q={1}", Url.Content(fileUrl), DateTime.Now.Ticks);
|
|
|
|
|
UserHelper.SaveUserIconByName(userName, fileName);
|
2018-06-24 16:16:58 +08:00
|
|
|
|
}
|
|
|
|
|
return new JsonResult(new
|
|
|
|
|
{
|
|
|
|
|
error = string.IsNullOrEmpty(error) ? error : $"服务器端错误-{error}",
|
|
|
|
|
initialPreview = new string[] { previewUrl },
|
|
|
|
|
initialPreviewConfig = new object[] {
|
2018-09-28 15:08:01 +08:00
|
|
|
|
new { caption = "新头像", size = fileSize, showZoom = true, key = fileName }
|
2018-06-24 16:16:58 +08:00
|
|
|
|
},
|
|
|
|
|
append = false
|
|
|
|
|
});
|
2018-06-07 00:45:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|