diff --git a/Tiobon.Core.Api/Controllers/FileController.cs b/Tiobon.Core.Api/Controllers/FileController.cs index cb9ad8f1..358bd470 100644 --- a/Tiobon.Core.Api/Controllers/FileController.cs +++ b/Tiobon.Core.Api/Controllers/FileController.cs @@ -12,15 +12,24 @@ public class FileController : BaseApiController { public ITiobonArticleServices _TiobonArticleServices { get; set; } private readonly ILogger _logger; - + /// + /// 配置信息 + /// + private readonly IConfiguration _configuration; + private readonly IWebHostEnvironment _hostingEnvironment; + private readonly IGhre_AttachmentServices _ghre_AttachmentServices; /// /// 构造函数 /// /// - /// - public FileController(ILogger logger) + /// + /// + public FileController(ILogger logger, IConfiguration configuration, IWebHostEnvironment hostingEnvironment, IGhre_AttachmentServices ghre_AttachmentServices) { _logger = logger; + _configuration = configuration; + _hostingEnvironment = hostingEnvironment; + _ghre_AttachmentServices = ghre_AttachmentServices; } #region 上传图片 @@ -80,4 +89,54 @@ public class FileController : BaseApiController } #endregion + + #region 获取图片 + [HttpGet("Download/{Id}"), AllowAnonymous] + public async Task Download(long Id) + { + try + { + var webRootPath = _hostingEnvironment.WebRootPath; + + using var _context = ContextFactory.CreateContext(); + //var file = await _context.Ghre_Attachment.Where(o => o.Id == Id).FirstOrDefaultAsync(); + var file = await _ghre_AttachmentServices.QueryById(Id); + if (file == null) + return Ok("找不到文件"); + + var filePath = $"{webRootPath}{"\\" + file.PhysicsPath}"; + + var contentTypDict = new Dictionary { + {"jpg","image/jpeg"}, + {"jpeg","image/jpeg"}, + {"jpe","image/jpeg"}, + {"png","image/png"}, + {"gif","image/gif"}, + {"ico","image/x-ico"}, + {"tif","image/tiff"}, + {"tiff","image/tiff"}, + {"fax","image/fax"}, + {"wbmp","image//vnd.wap.wbmp"}, + {"rp","image/vnd.rn-realpix"} + }; + var contentTypeStr = "image/jpeg"; + //未知的图片类型 + contentTypeStr = contentTypDict[file.AttachFileExtension]; + + using (var sw = new FileStream(filePath, FileMode.Open)) + { + var bytes = new byte[sw.Length]; + sw.Read(bytes, 0, bytes.Length); + sw.Close(); + return new FileContentResult(bytes, contentTypeStr); + } + + } + catch (Exception ex) + { + return Ok($"下载异常:{ex.Message}"); + } + } + #endregion + } \ No newline at end of file diff --git a/Tiobon.Core.Api/Tiobon.Core.xml b/Tiobon.Core.Api/Tiobon.Core.xml index f5346e86..08b68d8f 100644 --- a/Tiobon.Core.Api/Tiobon.Core.xml +++ b/Tiobon.Core.Api/Tiobon.Core.xml @@ -311,12 +311,18 @@ 文件服务 - + + + 配置信息 + + + 构造函数 - + +