[英]How to validate files with FluentValidation
I want to check with FluentValidation if the file I received with IFormFile has the image extension.我想通过 FluentValidation 检查我使用 IFormFile 收到的文件是否具有图像扩展名。 How can I do that?我怎样才能做到这一点?
you can do something like this你可以做这样的事情
public class FormFileValidator : AbstractValidator<IFormFile>
{
public FormFileValidator()
{
RuleFor(x => x.FileName).Must(a =>a.EndsWith(".png")) // put your logic based on extension you want.
.WithMessage("Given file is not a image type");
}
}
and then in your controller you can validate file like this.然后在您的控制器中,您可以像这样验证文件。
var result = new FormFileValidator().Validate(file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.