【发布时间】:2019-04-09 03:52:38
【问题描述】:
我想在我的 API 中验证我的图片上传。我只想允许处于横向模式的照片。我还想检查纵横比。这是我检查 iFormFile 是否为图像的代码:
[HttpPost]
public JsonResult Post(IFormFile file)
{
if (file.ContentType.ToLower() != "image/jpeg" &&
file.ContentType.ToLower() != "image/jpg" &&
file.ContentType.ToLower() != "image/png")
{
// not a .jpg or .png file
return new JsonResult(new
{
success = false
});
}
// todo: check aspect ratio for landscape mode
return new JsonResult(new
{
success = true
});
}
由于System.Drawing.Image 不再可用,我找不到将 iFormFile 转换为 Image 类型对象的方法,以检查宽度和高度以计算其纵横比。如何在 ASP.NET Core API 2.0 中获取 iFormFile 类型的图像的宽度和高度?
【问题讨论】:
-
你不应该仅仅为了获得它的尺寸而渲染一个文件,使用像 SoftImaging 这样的图像库来从文件中提取尺寸。
-
@DourHighArch 你不应该仅仅为了获取它的尺寸而渲染一个文件——这也许是对的,但是使用第三方库只是为了对图像进行简单的尺寸检查? - 我不认为这是要走的路。
标签: c# asp.net-core-2.0 asp.net-core-webapi