Przeglądaj źródła

图片上传和显示代码

shengxuefei 3 lat temu
rodzic
commit
5b84aa13f3

+ 0 - 12
PMS.NetCore/PMS.NetCore.sln

@@ -13,10 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Utils", "Utils\Utils.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PMS.NetCore.Service", "Service\PMS.NetCore.Service.csproj", "{0736B6D8-C94E-49E3-AF9C-E9C4909AEE6F}"
 EndProject
-Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UtilsTests", "UtilsTests\UtilsTests.csproj", "{24E61C72-AB99-4BAE-BC67-302FAD4B6029}"
-EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PMS.NetCore.BusinessCoreTests", "PMS.NetCore.BusinessCoreTests\PMS.NetCore.BusinessCoreTests.csproj", "{37C5FF73-D9D1-464D-A1E2-0F37C1334286}"
-EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -43,14 +39,6 @@ Global
 		{0736B6D8-C94E-49E3-AF9C-E9C4909AEE6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{0736B6D8-C94E-49E3-AF9C-E9C4909AEE6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{0736B6D8-C94E-49E3-AF9C-E9C4909AEE6F}.Release|Any CPU.Build.0 = Release|Any CPU
-		{24E61C72-AB99-4BAE-BC67-302FAD4B6029}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{24E61C72-AB99-4BAE-BC67-302FAD4B6029}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{24E61C72-AB99-4BAE-BC67-302FAD4B6029}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{24E61C72-AB99-4BAE-BC67-302FAD4B6029}.Release|Any CPU.Build.0 = Release|Any CPU
-		{37C5FF73-D9D1-464D-A1E2-0F37C1334286}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
-		{37C5FF73-D9D1-464D-A1E2-0F37C1334286}.Debug|Any CPU.Build.0 = Debug|Any CPU
-		{37C5FF73-D9D1-464D-A1E2-0F37C1334286}.Release|Any CPU.ActiveCfg = Release|Any CPU
-		{37C5FF73-D9D1-464D-A1E2-0F37C1334286}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 18 - 37
PMS.NetCore/PMS.NetCore/Controllers/SystemConfigurationController.cs

@@ -10,6 +10,7 @@ using Utils;
 using Microsoft.AspNetCore.Authorization;
 using Microsoft.AspNetCore.Http;
 using System.IO;
+using MimeMapping;
 
 namespace PMS.NetCore.Controllers
 {
@@ -1203,13 +1204,7 @@ namespace PMS.NetCore.Controllers
                     #region 存文件到文件夹
                     var filePath = savePath;
                     
-                    //获取日期路径
-                    var creatDate = DateTime.Now.ToString("yyyyMMDD");
-                    filePath += $"/{creatDate}";
-                    if (!Directory.Exists(filePath))    //若文件目录不存在,则创建目录
-                    {
-                        Directory.CreateDirectory(filePath);
-                    }
+                    //获取日期路径                   
                     filePath += $"/{file.FileName}";
                     using (var sw = System.IO.File.Open(filePath, FileMode.Create, FileAccess.Write))
                     {
@@ -1246,41 +1241,22 @@ namespace PMS.NetCore.Controllers
         /// <summary>
         /// 附件显示
         /// </summary>
-        /// <param name="code"></param>
+        /// <param name="autoid"></param>
         /// <returns></returns>
         [AllowAnonymous]
         [HttpGet]
         [Route("ShowImg")]
-        public FileResult ShowImg(string code)
+        public FileResult ShowImg(string autoid)
         {
-            var bll = new AttachMentManager();
-            var entity = bll.GetAttachMent(code);
-            if (entity.Content != null)     //从数据库读取
-            {
-                return File(entity.Content, entity.Content_Type);
-            }
-            else                           //从路径读取
+            var bll = new fdc_pm_fileManager();
+            var entity = bll.GetList(m => m.autoid == autoid.ToInteger()).FirstOrDefault();
+            if (entity.fileurl != null)     //从数据库读取
             {
                 var filePath = savePath;
-                var filename = entity.GuidName;
-                //获取日期路径
-                var creatDate = entity.CreateDate.Value.ToString("yyyyMMDD");
-                if (pathPattern == "Root")
-                {
-                    filePath += $"/{filename}";
-                }
-                else if (pathPattern == "YYYY")
-                {
-                    filePath += $"/{creatDate.Substring(0, 4)}/{filename}";
-                }
-                else if (pathPattern == "YYYYMM")
-                {
-                    filePath += $"/{creatDate.Substring(0, 6)}/{filename}";
-                }
-                else if (pathPattern == "YYYYMMDD")
-                {
-                    filePath += $"/{creatDate}/{filename}";
-                }
+                var filename = entity.fileurl;
+                
+               filePath += $"/{filePath}/{filename}";
+                
                 logger.Info($"附件保存路径:{filePath}");
                 if (!System.IO.File.Exists(filePath))
                 {
@@ -1290,11 +1266,16 @@ namespace PMS.NetCore.Controllers
                 byte[] infbytes = new byte[(int)fs.Length];
                 fs.Read(infbytes, 0, infbytes.Length);
                 fs.Close();
-                var contentType = entity.Content_Type;
+                var contentType =MimeUtility.GetMimeMapping(filePath);
                 return File(infbytes, contentType);
+                 
+            }
+            else
+            {
+                RedirectToAction("NotFound");
             }
 
-
+            return null;
         }
         #endregion
     }

+ 2 - 0
PMS.NetCore/PMS.NetCore/PMS.NetCore.csproj

@@ -27,6 +27,7 @@
     <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.3" />
     <PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.3" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.3" />
+    <PackageReference Include="MimeMapping" Version="1.0.1.37" />
     <PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
   </ItemGroup>
 
@@ -37,6 +38,7 @@
 
   <ItemGroup>
     <Folder Include="Logs\" />
+    <Folder Include="uploadimg\" />
   </ItemGroup>
 
 </Project>