Browse Source

获取图片

wangyuhan 1 year ago
parent
commit
1a9880fb3c
1 changed files with 58 additions and 4 deletions
  1. 58 4
      src/PTCMS.API/PTCMS.API/Controllers/CertificateController.cs

+ 58 - 4
src/PTCMS.API/PTCMS.API/Controllers/CertificateController.cs

@@ -1,4 +1,6 @@
-using Microsoft.AspNetCore.Http;
+using System.Security.Policy;
+
+using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 
 using PTCMS.API.Domain.Repositories;
@@ -14,14 +16,17 @@ namespace PTCMS.API.Controllers
     public class CertificateController : ControllerBase
     {
         private readonly ICertificateRepository _certificateRepository;
+        private readonly IHostEnvironment _hostEnvironment;
 
         /// <summary>
         /// 注入服务
         /// </summary>
         /// <param name="certificateRepository"></param>
-        public CertificateController(ICertificateRepository certificateRepository)
+        /// <param name="hostEnvironment"></param>
+        public CertificateController(ICertificateRepository certificateRepository, IHostEnvironment hostEnvironment)
         {
             _certificateRepository = certificateRepository;
+            _hostEnvironment = hostEnvironment;
         }
 
         /// <summary>
@@ -32,9 +37,58 @@ namespace PTCMS.API.Controllers
         [HttpPost]
         public async Task<IActionResult> GetCertificateAsync(CertificateSearchDto input)
         {
-            var data = await _certificateRepository.GetCertificateAsync(input);
+            try
+            {
+                var data = await _certificateRepository.GetCertificateAsync(input);
+
+                return Ok(data);
+            }
+            catch (Exception ex)
+            {
+                return BadRequest(ex.Message);
+            }
+        }
+
+        /// <summary>
+        /// 获取图片
+        /// </summary>
+        /// <returns></returns>
+        [HttpGet]
+        public IActionResult ShowImage(string url)
+        {
+            if (string.IsNullOrWhiteSpace(url))
+                return BadRequest("文件名无效");
+
+
+
+            var ext = Path.GetExtension(url);
+            if (ext == ".jpg" || ext == ".png")
+            {
+                var path = Path.Combine(_hostEnvironment.ContentRootPath, "Images");
+
+                try
+                {
+                    if (!Directory.Exists(path))
+                    {
+                        Directory.CreateDirectory(path);
+                    }
+                    using (var sw = new FileStream(Path.Combine(path, url), FileMode.Open))
+                    {
+                        var bytes = new byte[sw.Length];
+                        sw.Read(bytes, 0, bytes.Length);
+                        sw.Close();
+                        return new FileContentResult(bytes, "image/jpeg");
+                    }
+                }
+                catch (Exception ex)
+                {
+                    return BadRequest(ex.Message);
+                }
+               
+            };
+
+            return BadRequest("目前只支持jpg和png格式");
 
-            return Ok(data);
         }
     }
 }