You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.1 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Txgy.EWS.Server.ICommon;
namespace Txgy.EWS.Server.Start.Controllers
{
[ApiController]
public class ImageController : ControllerBase
{
IConfiguration _configuration;
public ImageController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet("show/{img}")]
public IActionResult GetImage([FromRoute(Name = "img")] string imgPath)
{
string rootPath = _configuration.Read("FileFolder");
//获取图片的返回类型
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/nd.wap.wbmp"},
{"rp","imagend.rn-realpix"}
};
var contentTypeStr = "image/jpeg";
var imgTypeSplit = imgPath.Split('.');
var imgType = imgTypeSplit[imgTypeSplit.Length - 1].ToLower();
//未知的图片类型
if (contentTypDict.ContainsKey(imgType))
{
contentTypeStr = contentTypDict[imgType];
}
string filePath = Path.Combine(rootPath, imgPath);
//图片不存在
if (!new FileInfo(filePath).Exists)
{
return NoContent();
}
//返回原图
using (var sw = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var bytes = new byte[sw.Length];
sw.Read(bytes, 0, bytes.Length);
sw.Close();
return new FileContentResult(bytes, contentTypeStr);
}
}
}
}