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