validating file upload asp.net core mvc
public class AllowedExtensionsAttribute:ValidationAttribute
{
private readonly string[] _Extensions;
public AllowedExtensionsAttribute(string[] Extensions)
{
_Extensions = Extensions;
}
protected override ValidationResult IsValid(
object value, ValidationContext validationContext)
{
var file = value as IFormFile;
var extension = Path.GetExtension(file.FileName);
if (!(file == null))
{
if (!_Extensions.Contains(extension.ToLower()))
{
return new ValidationResult(GetErrorMessage());
}
}
return ValidationResult.Success;
}
public string GetErrorMessage()
{
return $"This photo extension is not allowed!";
}
}public class UserViewModel
{
[Required(ErrorMessage = "Please select a file.")]
[DataType(DataType.Upload)]
[MaxFileSize(5* 1024 * 1024)]
[AllowedExtensions(new string[] { ".jpg", ".png" })]
public IFormFile Photo { get; set; }
}
Also in C#:
- unity separator in inspector
- c# declare an int list
- how to update modal class using dbfirst in asp.net core
- Microsoft.AspNetCore.Components.Forms.InputSelect`1.TryParseValueFromString(String value, TValue& result, String& validationErrorMessage)
- add row count devepxress report
- switch case c# contains
- covert char[] to string C#
- c# loop through array
- unity scene name get
- inheritance in c#
- c# lambda expression
- array copy c#
- why is c# say ; expected
- c# get hwid
- camera follow
- unity random float
- json ignore property c#
- stateteach.net
- get what week of the month c#
- c# events and delegates
- c# length of array
- c# how to add newline on text box
- get list length c#
- c# post request