From ce1f8c5b0e8cd5d4050e79c3e02433aafce81b24 Mon Sep 17 00:00:00 2001
From: huangxiaoqiang <huangxiaoqiang@hnkhzn.com>
Date: 星期二, 09 十二月 2025 16:16:22 +0800
Subject: [PATCH] 1

---
 Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs |   88 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 86 insertions(+), 2 deletions(-)

diff --git a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
index 475d7ec..633a59c 100644
--- a/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
+++ b/Code Management/WMS/WIDESEA_WMSServer/WIDESEA_WMSServer/Controllers/System/Sys_LogController.cs
@@ -1,6 +1,5 @@
 锘縰sing Microsoft.AspNetCore.Mvc;
 using System.Text;
-using System.IO;
 
 namespace WIDESEA_WMSServer.Controllers.System
 {
@@ -81,7 +80,92 @@
             }
             return content;
         }
+        [HttpPost, HttpGet, Route("DownLoadLog"), AllowAnonymous]
+        public virtual ActionResult DownLoadLog(string fileName)
+        {
+            try
+            {
+                // 1. 鍙傛暟楠岃瘉
+                if (string.IsNullOrWhiteSpace(fileName))
+                {
+                    return BadRequest("鏂囦欢鍚嶄笉鑳戒负绌�");
+                }
 
-        
+                string logDirectory = Path.Combine(AppContext.BaseDirectory, "Logs");
+
+                if (!Directory.Exists(logDirectory))
+                {
+                    Directory.CreateDirectory(logDirectory);
+                }
+
+                string filePath = Path.Combine(logDirectory, fileName);
+
+                if (Directory.Exists(filePath))
+                {
+                    return NotFound($"鏂囦欢 {fileName} 涓嶅瓨鍦�");
+                }
+
+                string extension = Path.GetExtension(fileName).ToLowerInvariant();
+                if (!IsAllowedFileType(extension))
+                {
+                    return BadRequest($"涓嶆敮鎸佺殑鏂囦欢绫诲瀷: {extension}");
+                }
+
+                FileInfo fileInfo = new FileInfo(filePath);
+
+                if (fileInfo.Length > 50 * 1024 * 1024) // 50MB闄愬埗
+                {
+                    return BadRequest("鏂囦欢杩囧ぇ锛屾棤娉曚笅杞�");
+                }
+
+                byte[] fileBytes;
+                using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
+                {
+                    using (MemoryStream memoryStream = new MemoryStream())
+                    {
+                        fileStream.CopyTo(memoryStream);
+                        fileBytes = memoryStream.ToArray();
+                    }
+                }
+
+                string contentType = GetContentType(extension);
+
+                return File(fileBytes, contentType, fileName);
+            }
+            catch (UnauthorizedAccessException)
+            {
+                return StatusCode(403, "娌℃湁璁块棶璇ユ枃浠剁殑鏉冮檺");
+            }
+            catch (PathTooLongException)
+            {
+                return BadRequest("鏂囦欢璺緞杩囬暱");
+            }
+            catch (IOException ex)
+            {
+                return StatusCode(500, "鏂囦欢璇诲彇澶辫触锛屽彲鑳芥鍦ㄨ鍏朵粬杩涚▼浣跨敤");
+            }
+            catch (Exception ex)
+            {
+                return StatusCode(500, $"鏈嶅姟鍣ㄥ唴閮ㄩ敊璇�: {ex.Message}");
+            }
+        }
+        private bool IsAllowedFileType(string extension)
+        {
+            var allowedTypes = new[] { ".txt", ".log", ".csv", ".json", ".xml" };
+            return allowedTypes.Contains(extension);
+        }
+
+        private string GetContentType(string extension)
+        {
+            return extension.ToLowerInvariant() switch
+            {
+                ".txt" => "text/plain; charset=utf-8",
+                ".log" => "text/plain; charset=utf-8",
+                ".csv" => "text/csv; charset=utf-8",
+                ".json" => "application/json; charset=utf-8",
+                ".xml" => "application/xml; charset=utf-8",
+                _ => "application/octet-stream"
+            };
+        }
     }
 }

--
Gitblit v1.9.3