文件服务 新增获取图片接口(主键ID)

master
xiaochanghai 1 year ago
parent 2f56152fab
commit dd1a008166
  1. 65
      Tiobon.Core.Api/Controllers/FileController.cs
  2. 10
      Tiobon.Core.Api/Tiobon.Core.xml

@ -12,15 +12,24 @@ public class FileController : BaseApiController
{
public ITiobonArticleServices _TiobonArticleServices { get; set; }
private readonly ILogger<TiobonController> _logger;
/// <summary>
/// 配置信息
/// </summary>
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IGhre_AttachmentServices _ghre_AttachmentServices;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="logger"></param>
///
public FileController(ILogger<TiobonController> logger)
/// <param name="configuration"></param>
/// <param name="hostingEnvironment"></param>
public FileController(ILogger<TiobonController> 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<IActionResult> 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<string, string> {
{"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
}

@ -311,12 +311,18 @@
文件服务
</summary>
</member>
<member name="M:Tiobon.Core.Controllers.FileController.#ctor(Microsoft.Extensions.Logging.ILogger{Tiobon.Core.Controllers.TiobonController})">
<member name="F:Tiobon.Core.Controllers.FileController._configuration">
<summary>
配置信息
</summary>
</member>
<member name="M:Tiobon.Core.Controllers.FileController.#ctor(Microsoft.Extensions.Logging.ILogger{Tiobon.Core.Controllers.TiobonController},Microsoft.Extensions.Configuration.IConfiguration,Microsoft.AspNetCore.Hosting.IWebHostEnvironment,Tiobon.Core.IServices.IGhre_AttachmentServices)">
<summary>
构造函数
</summary>
<param name="logger"></param>
<param name="configuration"></param>
<param name="hostingEnvironment"></param>
</member>
<member name="M:Tiobon.Core.Controllers.FileController.UploadImageAsync(Microsoft.AspNetCore.Http.IFormFileCollection)">
<summary>

Loading…
Cancel
Save