Skip to content

Commit

Permalink
增加上传头像接口
Browse files Browse the repository at this point in the history
  • Loading branch information
xxred committed Aug 20, 2023
1 parent aaa53ac commit 73404fa
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
53 changes: 52 additions & 1 deletion NewLife.Cube/Areas/Admin/Controllers/FileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ namespace NewLife.Cube.Areas.Admin.Controllers;
[Menu(28, false, Icon = "fa-file")]
public class FileController : ControllerBaseX
{
#region 构造
private IWebHostEnvironment _env;

/// <summary>构造函数</summary>
public FileController(IWebHostEnvironment env) => _env = env;
#endregion

#region 基础
private String Root => "../".GetCurrentPath();

Expand Down Expand Up @@ -275,7 +282,50 @@ public async Task<ActionResult> UploadLayui(String r, IFormFile file)
using var fs = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
await file.CopyToAsync(fs);
}
return Json(0, "上传成功");
return Json(0, "上传成功", r.EnsureStart("/").EnsureEnd("/") + file.FileName);
}
catch (Exception ex)
{
WriteLog("上传失败", false, ex + "");
return Json(500, "上传失败");
}
}

/// <summary>上传头像</summary>
/// <param name="file"></param>
/// <returns></returns>
[HttpPost]
[EntityAuthorize(PermissionFlags.Insert)]
public async Task<ActionResult> UploadAvatar(IFormFile file)
{

// 判断文件是否属于图片
if (!file.ContentType.Contains("image"))
{
return Json(500, "只能上传图片文件");
}

try
{
var set = CubeSetting.Current;
var r = set.AvatarPath.EnsureEnd("/");
var root = _env.ContentRootPath;
var path = root.CombinePath(set.AvatarPath);

if (file != null)
{
var di = path.AsDirectory();
if (di == null) throw new Exception("找不到目录!");

var dest = di.FullName.CombinePath(file.FileName);
WriteLog("上传", true, dest);

dest.EnsureDirectory(true);
//System.IO.File.WriteAllBytes(dest, file.OpenReadStream().ReadBytes(-1));
using var fs = new FileStream(dest, FileMode.OpenOrCreate, FileAccess.ReadWrite);
await file.CopyToAsync(fs);
}
return Json(0, "上传成功", r.EnsureStart("/").EnsureEnd("/") + file.FileName);
}
catch (Exception ex)
{
Expand All @@ -284,6 +334,7 @@ public async Task<ActionResult> UploadLayui(String r, IFormFile file)
}
}


/// <summary>下载文件</summary>
/// <param name="r"></param>
/// <returns></returns>
Expand Down
19 changes: 17 additions & 2 deletions NewLife.Cube/CubeService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Reflection;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Encodings.Web;
using System.Text.Unicode;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationParts;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.WebEncoders;
using Microsoft.Net.Http.Headers;
using NewLife.Common;
Expand Down Expand Up @@ -179,7 +181,20 @@ public static IApplicationBuilder UseCube(this IApplicationBuilder app, IWebHost
app.UseStaticHttpContext();

// 注册中间件
//app.UseStaticFiles();

// 如果,头像目录设置不为空,开启静态文件中间件
if (!set.AvatarPath.IsNullOrWhiteSpace())
{
var root = env.ContentRootPath;
var path = root.CombinePath(set.AvatarPath);
path.EnsureDirectory(false);
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(path),
RequestPath = set.AvatarPath.EnsureStart("/")
});
}

app.UseCookiePolicy();
//app.UseSession();
app.UseAuthentication();
Expand Down

0 comments on commit 73404fa

Please sign in to comment.