优化文件存储
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using Infrastructure;
|
using Infrastructure;
|
||||||
using Infrastructure.Attribute;
|
using Infrastructure.Attribute;
|
||||||
using Infrastructure.Enums;
|
using Infrastructure.Enums;
|
||||||
|
using Infrastructure.Extensions;
|
||||||
using Infrastructure.Model;
|
using Infrastructure.Model;
|
||||||
using Microsoft.AspNetCore.Hosting;
|
using Microsoft.AspNetCore.Hosting;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
@@ -94,7 +95,7 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
[HttpPost()]
|
[HttpPost()]
|
||||||
[Verify]
|
[Verify]
|
||||||
[ActionPermissionFilter(Permission = "common")]
|
[ActionPermissionFilter(Permission = "common")]
|
||||||
public async Task<IActionResult> UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "uploads", StoreType storeType = StoreType.LOCAL)
|
public async Task<IActionResult> UploadFile([FromForm(Name = "file")] IFormFile formFile, string fileName = "", string fileDir = "", StoreType storeType = StoreType.LOCAL)
|
||||||
{
|
{
|
||||||
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空");
|
||||||
SysFile file = new();
|
SysFile file = new();
|
||||||
@@ -109,8 +110,12 @@ namespace ZR.Admin.WebApi.Controllers
|
|||||||
switch (storeType)
|
switch (storeType)
|
||||||
{
|
{
|
||||||
case StoreType.LOCAL:
|
case StoreType.LOCAL:
|
||||||
file = await SysFileService.SaveFileToLocal(WebHostEnvironment.WebRootPath, fileName, fileDir, HttpContext.GetName(), formFile);
|
string savePath = AppSettings.App(new string[] { "Upload", "localSavePath" });
|
||||||
|
if (savePath.IsEmpty())
|
||||||
|
{
|
||||||
|
savePath = Path.Combine(WebHostEnvironment.WebRootPath, "uploads");
|
||||||
|
}
|
||||||
|
file = await SysFileService.SaveFileToLocal(savePath, fileName, fileDir, HttpContext.GetName(), formFile);
|
||||||
break;
|
break;
|
||||||
case StoreType.ALIYUN:
|
case StoreType.ALIYUN:
|
||||||
if ((fileSize / 1024) > MaxContentLength)
|
if ((fileSize / 1024) > MaxContentLength)
|
||||||
|
|||||||
@@ -20,7 +20,8 @@
|
|||||||
},
|
},
|
||||||
"DemoMode": false, //<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ʾģʽ
|
"DemoMode": false, //<2F>Ƿ<EFBFBD><C7B7><EFBFBD>ʾģʽ
|
||||||
"Upload": {
|
"Upload": {
|
||||||
"UploadUrl": "http://localhost:8888"
|
"UploadUrl": "http://localhost:8888",
|
||||||
|
"localSavePath": "" //<2F><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4>ļ<EFBFBD><C4BC>洢Ŀ¼/home/website/uploads
|
||||||
},
|
},
|
||||||
//<2F><><EFBFBD><EFBFBD><EFBFBD>ƴ洢<C6B4><E6B4A2><EFBFBD><EFBFBD>
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ƴ洢<C6B4><E6B4A2><EFBFBD><EFBFBD>
|
||||||
"ALIYUN_OSS": {
|
"ALIYUN_OSS": {
|
||||||
|
|||||||
@@ -34,12 +34,17 @@ namespace ZR.Service.System
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 存储本地
|
/// 存储本地
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="fileDir">存储文件夹</param>
|
||||||
|
/// <param name="rootPath">存储根目录</param>
|
||||||
|
/// <param name="fileName">自定文件名</param>
|
||||||
|
/// <param name="formFile">上传的文件流</param>
|
||||||
|
/// <param name="userName"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public async Task<SysFile> SaveFileToLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
|
public async Task<SysFile> SaveFileToLocal(string rootPath, string fileName, string fileDir, string userName, IFormFile formFile)
|
||||||
{
|
{
|
||||||
string fileExt = Path.GetExtension(formFile.FileName);
|
string fileExt = Path.GetExtension(formFile.FileName);
|
||||||
fileName = (fileName.IsEmpty() ? HashFileName() : fileName) + fileExt;
|
fileName = (fileName.IsEmpty() ? HashFileName() : fileName) + fileExt;
|
||||||
fileDir = fileDir.IsEmpty() ? "uploads" : fileDir;
|
|
||||||
string filePath = GetdirPath(fileDir);
|
string filePath = GetdirPath(fileDir);
|
||||||
string finalFilePath = Path.Combine(rootPath, filePath, fileName);
|
string finalFilePath = Path.Combine(rootPath, filePath, fileName);
|
||||||
double fileSize = Math.Round(formFile.Length / 1024.0, 2);
|
double fileSize = Math.Round(formFile.Length / 1024.0, 2);
|
||||||
@@ -51,7 +56,7 @@ namespace ZR.Service.System
|
|||||||
|
|
||||||
using (var stream = new FileStream(finalFilePath, FileMode.Create))
|
using (var stream = new FileStream(finalFilePath, FileMode.Create))
|
||||||
{
|
{
|
||||||
await formFile.CopyToAsync(stream);//await 不能少
|
await formFile.CopyToAsync(stream);
|
||||||
}
|
}
|
||||||
string uploadUrl = SysConfigService.GetSysConfigByKey("sys.file.uploadUrl")?.ConfigValue ?? OptionsSetting.Upload.UploadUrl;
|
string uploadUrl = SysConfigService.GetSysConfigByKey("sys.file.uploadUrl")?.ConfigValue ?? OptionsSetting.Upload.UploadUrl;
|
||||||
string accessPath = string.Concat(uploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
|
string accessPath = string.Concat(uploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName);
|
||||||
@@ -59,7 +64,7 @@ namespace ZR.Service.System
|
|||||||
{
|
{
|
||||||
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
|
StoreType = (int)Infrastructure.Enums.StoreType.LOCAL,
|
||||||
FileType = formFile.ContentType,
|
FileType = formFile.ContentType,
|
||||||
FileUrl = finalFilePath,
|
FileUrl = finalFilePath.Replace("\\", "/"),
|
||||||
AccessUrl = accessPath
|
AccessUrl = accessPath
|
||||||
};
|
};
|
||||||
file.Id = await InsertFile(file);
|
file.Id = await InsertFile(file);
|
||||||
|
|||||||
Reference in New Issue
Block a user