gpt4 book ai didi

c# - IFormFile 的数据注释,因此它只允许扩展名为 .png、.jpg 或 .jpeg 的文件

转载 作者:行者123 更新时间:2023-12-03 23:43:16 25 4
gpt4 key购买 nike

我正在使用 ASP.NET Core MVC。我需要 IFormFile 的数据注释/自定义数据注释,检查选择/上传的文件是图像,图像的扩展名必须匹配*.png , *.jpg*.jpeg .这是 View 模型

public class ProductViewModel 
{
[Display(Name = "Image")]
[Required(ErrorMessage = "Pick an Image")]
//[CheckIfItsAnImage(ErrorMessage = "The file selected/uploaded is not an image")]
public IFormFile File { get; set; }
}

最佳答案

Chameera 与他的回答非常接近,但不幸的是内置的 FileExtensions DataAnnotation仅适用于字符串而不适用于 IFormFile。这是解决方案:

public class ProductViewModel 
{
[Display(Name = "Image")]
[Required(ErrorMessage = "Pick an Image")]
public IFormFile File { get; set; }

[FileExtensions(Extensions = "jpg,jpeg")]
public string FileName => File?.FileName;
}

关于c# - IFormFile 的数据注释,因此它只允许扩展名为 .png、.jpg 或 .jpeg 的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64518949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com