validating file upload asp.net core mvc
C#
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#:
- Title
- disable mouse unity
- Category
- C#
- Title
- c# convert column name to number
- Category
- C#
- Title
- webclient c# example post
- Category
- C#
- Title
- Request.Form
- Category
- C#
- Title
- using mediamanager how to play mp3 files
- Category
- C#
- Title
- C# .NET Core linq Distinct
- Category
- C#
- Title
- c# udpclient receive buffer size
- Category
- C#
- Title
- c# loop datatable rows
- Category
- C#
- Title
- if get key down unity
- Category
- C#
- Title
- c# replace foreach with lambda
- Category
- C#
- Title
- C# sprint key
- Category
- C#
- Title
- index in foreach c#
- Category
- C#
- Title
- change Backcolor c#
- Category
- C#
- Title
- membership get user id
- Category
- C#
- Title
- c# how to run external program
- Category
- C#
- Title
- how to reference scripts in other scenes unity
- Category
- C#
- Title
- unity up arrow input
- Category
- C#
- Title
- unity how to tell when a gameobject is colliding
- Category
- C#
- Title
- two players one phone unity gamne
- Category
- C#
- Title
- c# sql duplicate key exception
- Category
- C#
- Title
- unity access child
- Category
- C#
- Title
- c# round number
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- 2d object look at object
- Category
- C#
- Title
- how to get joypad axis input unity
- Category
- C#
- Title
- C# resize window without title bar
- Category
- C#
- Title
- max of array C#
- Category
- C#
- Title
- unity indestructible
- Category
- C#
- Title
- c# exit program
- Category
- C#
- Title
- polling data source c# using threads
- Category
- C#
- Title
- response redirect new tab
- Category
- C#
- Title
- c# postfix increment operator overload
- Category
- C#
- Title
- c# filter non alphanumeric characters
- Category
- C#
- Title
- jumping with character controller unity
- Category
- C#
- Title
- how to clear console through script unity
- Category
- C#
- Title
- c# stop loop
- Category
- C#
- Title
- epplus excel vb.net
- Category
- C#
- Title
- repeater itemdatabound event in c#
- Category
- C#
- Title
- how to get array of children transform
- Category
- C#
- Title
- add spaces between words unity
- Category
- C#
- Title
- unity how to set an objects postion x,y,z
- Category
- C#
- Title
- array c#
- Category
- C#
- Title
- parse strings into words C#
- Category
- C#
- Title
- c# verify in class exist in list
- Category
- C#
- Title
- how to create more accurate searching c#
- Category
- C#
- Title
- unity docs player input
- Category
- C#
- Title
- c# map
- Category
- C#
- Title
- if a method doesnt rutern a value, what shouyld i reeturn c#
- Category
- C#
- Title
- c# string to lowercase
- Category
- C#
- Title
- c# reverse string
- Category
- C#
- Title
- change partial view based on select asp.net core
- Category
- C#
- Title
- c sharp if string equals
- Category
- C#
- Title
- c# cheat sheet
- Category
- C#
- Title
- sort datatable c#
- Category
- C#
- Title
- c# get enum in list
- Category
- C#
- Title
- install .net sdk ubuntu 20
- Category
- C#
- Title
- order by C#
- Category
- C#
- Title
- defualtsize UWP c#
- Category
- C#
- Title
- csharp check if env is development
- Category
- C#
- Title
- c# mailmessage set sender name
- Category
- C#
- Title
- c# making a folder
- Category
- C#
- Title
- unity how to change max fps
- Category
- C#
- Title
- query associative table ef6
- Category
- C#
- Title
- landscape print gridcontrol devexpress
- Category
- C#
- Title
- binding c#
- Category
- C#
- Title
- C# for
- Category
- C#
- Title
- c# get date without time
- Category
- C#
- Title
- c# create array of int
- Category
- C#
- Title
- Update data in db .net
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- unity input get axis
- Category
- C#
- Title
- c# variable
- Category
- C#
- Title
- c# summary tag
- Category
- C#
- Title
- c# update control from another thread
- Category
- C#
- Title
- defining vectors in c#
- Category
- C#
- Title
- how to trim path in C#
- Category
- C#
- Title
- c# object clone
- Category
- C#
- Title
- delay in unity
- Category
- C#
- Title
- how to initiate a varaible in cs
- Category
- C#
- Title
- sum of digits in c#
- Category
- C#
- Title
- c# file exist
- Category
- C#
- Title
- office open xml check if row is empty
- Category
- C#
- Title
- c# append array
- Category
- C#
- Title
- unity delete all children
- Category
- C#
- Title
- return random from enum
- Category
- C#
- Title
- c# shorten an definition
- Category
- C#
- Title
- example HttpClient c# Post
- Category
- C#
- Title
- how to add movement in unity
- Category
- C#
- Title
- unity rb.addexplosionforce 2d
- Category
- C#
- Title
- c# append to file
- Category
- C#
- Title
- unity round
- Category
- C#
- Title
- rotatearound unity
- Category
- C#
- Title
- split string in c#
- Category
- C#
- Title
- how to add array to list in c#
- Category
- C#
- Title
- unity how to get transform scale
- Category
- C#
- Title
- .net core check if linux
- Category
- C#
- Title
- c# get full URL of page
- Category
- C#
- Title
- wpf c# select folder path
- Category
- C#
- Title
- c# select first value from list
- Category
- C#
- Title
- letter at index of string c#
- Category
- C#