瀏覽代碼

文件上传默认目录修改为 upload/yyyy/MM/dd

张甜 4 年之前
父節點
當前提交
bd9cb8f745
共有 2 個文件被更改,包括 12 次插入2 次删除
  1. 1 1
      Admin.NET/Admin.NET.Core/AdminNETConfig.json
  2. 11 1
      Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs

+ 1 - 1
Admin.NET/Admin.NET.Core/AdminNETConfig.json

@@ -39,7 +39,7 @@
     "WorkerId": 5 // 取值范围0~63,默认1
   },
   "Upload": {
-    "Path": "Upload",
+    "Path": "upload/{yyyy}/{MM}/{dd}", //文件上传目录
     "MaxSize": 1048576,
     "ContentType": [ "image/jpg", "image/png", "image/jpeg", "image/gif", "image/bmp", "text/plain", "application/pdf", "application/msword", "application/vnd.ms-excel", "application/vnd.ms-powerpoint", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" ]
   },

+ 11 - 1
Admin.NET/Admin.NET.Core/Service/File/SysFileService.cs

@@ -8,10 +8,13 @@ using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Options;
 using OnceMi.AspNetCore.OSS;
+using System;
 using System.Collections.Generic;
 using System.ComponentModel.DataAnnotations;
 using System.IO;
+using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Web;
 
@@ -160,7 +163,14 @@ namespace Admin.NET.Core.Service
         {
             if (file == null) throw Oops.Oh(ErrorCodeEnum.D8000);
 
-            string path = _uploadOptions.Path;
+            var path = _uploadOptions.Path;
+            Regex reg = new Regex(@"(\{.+?})");
+            var match = reg.Matches(path);
+            match.ToList().ForEach(a =>
+            {
+                var str = DateTime.Now.ToString(a.ToString().Substring(1, a.Length - 2));
+                path = path.Replace(a.ToString(), str);
+            });
 
             if (!_uploadOptions.ContentType.Contains(file.ContentType))
                 throw Oops.Oh(ErrorCodeEnum.D8001);