diff --git a/Infrastructure/Enums/StoreType.cs b/Infrastructure/Enums/StoreType.cs new file mode 100644 index 0000000..4dea7eb --- /dev/null +++ b/Infrastructure/Enums/StoreType.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Infrastructure.Enums +{ + /// + /// 文件存储位置 + /// + public enum StoreType + { + /// + /// 本地 + /// + [Description("本地")] + LOCAL = 1, + + /// + /// 阿里云 + /// + [Description("阿里云")] + ALIYUN = 2, + + /// + /// 腾讯云 + /// + [Description("腾讯云")] + TENCENT = 3, + + } +} diff --git a/Infrastructure/Helper/FileUtil.cs b/Infrastructure/Helper/FileUtil.cs index de8c701..8448ba2 100644 --- a/Infrastructure/Helper/FileUtil.cs +++ b/Infrastructure/Helper/FileUtil.cs @@ -16,7 +16,7 @@ namespace Infrastructure public static string GetdirPath(string path = "") { DateTime date = DateTime.Now; - string timeDir = Path.Combine(date.ToString("yyyyMMdd"));// date.ToString("yyyyMM/dd/HH/"); + string timeDir = date.ToString("yyyyMMdd");// date.ToString("yyyyMM/dd/HH/"); if (!string.IsNullOrEmpty(path)) { @@ -28,7 +28,7 @@ namespace Infrastructure /// /// 取文件名的MD5值(16位) /// - /// 文件名,不包括扩展名 + /// 文件名,不包括扩展名 /// public static string HashFileName(string str = null) { diff --git a/ZR.Admin.WebApi/Controllers/CommonController.cs b/ZR.Admin.WebApi/Controllers/CommonController.cs index ce579ef..28daeb1 100644 --- a/ZR.Admin.WebApi/Controllers/CommonController.cs +++ b/ZR.Admin.WebApi/Controllers/CommonController.cs @@ -110,13 +110,12 @@ namespace ZR.Admin.WebApi.Controllers { if (formFile == null) throw new CustomException(ResultCode.PARAM_ERROR, "上传文件不能为空"); string fileExt = Path.GetExtension(formFile.FileName); - string hashFileName = FileUtil.HashFileName(Guid.NewGuid().ToString()).ToLower(); + string hashFileName = FileUtil.HashFileName(); fileName = (fileName.IsEmpty() ? hashFileName : fileName) + fileExt; fileDir = fileDir.IsEmpty() ? "uploads" : fileDir; string filePath = FileUtil.GetdirPath(fileDir); string finalFilePath = Path.Combine(WebHostEnvironment.WebRootPath, filePath, fileName); - finalFilePath = finalFilePath.Replace("\\", "/").Replace("//", "/"); - double fileSize = formFile.Length / 1024; + double fileSize = formFile.Length / 1024.0; if (!Directory.Exists(Path.GetDirectoryName(finalFilePath))) { @@ -128,18 +127,10 @@ namespace ZR.Admin.WebApi.Controllers formFile.CopyTo(stream); } - string accessPath = $"{OptionsSetting.Upload.UploadUrl}/{filePath.Replace("\\", " /")}{fileName}"; - SysFile file = new() + string accessPath = string.Concat(OptionsSetting.Upload.UploadUrl, "/", filePath.Replace("\\", "/"), "/", fileName); + SysFile file = new(formFile.FileName, fileName, fileExt, fileSize + "kb", filePath, accessPath, HttpContext.GetName()) { - AccessUrl = accessPath, - Create_by = HttpContext.GetName(), - FileExt = fileExt, - FileName = fileName, - FileSize = fileSize + "kb", - StoreType = 1, - FileUrl = finalFilePath, - RealName = formFile.FileName, - Create_time = DateTime.Now, + StoreType = (int)Infrastructure.Enums.StoreType.LOCAL, FileType = formFile.ContentType }; long fileId = SysFileService.InsertFile(file); @@ -183,17 +174,9 @@ namespace ZR.Admin.WebApi.Controllers { result = SysFileService.SaveFile(fileDir, formFile, fileName, ""); }); - long id = SysFileService.InsertFile(new SysFile() + long id = SysFileService.InsertFile(new(formFile.FileName, fileName, fileExt, fileSize + "kb", "", result.Item2, HttpContext.GetName()) { - AccessUrl = result.Item2, - Create_by = HttpContext.GetName(), - FileExt = fileExt, - FileName = result.Item3, - FileSize = fileSize + "kb", - StoreType = 2, - StorePath = fileDir, - RealName = formFile.FileName, - Create_time = DateTime.Now, + StoreType = (int)Infrastructure.Enums.StoreType.ALIYUN, FileType = formFile.ContentType }); return SUCCESS(new diff --git a/ZR.Model/System/SysFile.cs b/ZR.Model/System/SysFile.cs index f229cab..08a2fd4 100644 --- a/ZR.Model/System/SysFile.cs +++ b/ZR.Model/System/SysFile.cs @@ -70,5 +70,18 @@ namespace ZR.Model.System /// 空值 : true /// public string AccessUrl { get; set; } + + public SysFile() { } + public SysFile(string realName, string fileName, string ext, string fileSize, string storePath, string accessUrl,string create_by) + { + StorePath = storePath; + RealName = realName; + FileName = fileName; + FileExt = ext; + FileSize = fileSize; + AccessUrl = accessUrl; + Create_by = create_by; + Create_time = DateTime.Now; + } } } \ No newline at end of file diff --git a/ZR.Vue/src/views/tool/file/index.vue b/ZR.Vue/src/views/tool/file/index.vue index 49c2a54..c061328 100644 --- a/ZR.Vue/src/views/tool/file/index.vue +++ b/ZR.Vue/src/views/tool/file/index.vue @@ -113,7 +113,12 @@ {{formView.id}} - {{formView.fileExt}} + {{formView.fileType}} + + + + {{formView.fileExt}} + {{formView.realName}} @@ -130,12 +135,9 @@ {{formView.create_by}} - - {{formView.fileUrl}} - - + @@ -146,6 +148,9 @@ + + {{formView.fileUrl}} +