I am trying to upload a file using HTML FileUpload control in MVC. I want to validate the file to accept only specific extensions. I have tried using FileExtensions attribute of DataAnnotations namespace, but its not working. See code below -
public class FileUploadModel
{
[Required, FileExtensions(Extensions = (".xlsx,.xls"), ErrorMessage = "Please select an Excel file.")]
public HttpPostedFileBase File { get; set; }
}
In the controller, I am writing the code as below -
[HttpPost]
public ActionResult Index(FileUploadModel fileUploadModel)
{
if (ModelState.IsValid)
fileUploadModel.File.SaveAs(Path.Combine(Server.MapPath("~/UploadedFiles"), Path.GetFileName(fileUploadModel.File.FileName)));
return View();
}
In View, I have written below code -
@using (Html.BeginForm("Index", "FileParse", FormMethod.Post, new { enctype = "multipart/form-data"} ))
{
@Html.Label("Upload Student Excel:")
<input type="file" name="file" id="file"/>
<input type="submit" value="Import"/>
@Html.ValidationMessageFor(m => m.File)
}
When i run the application and give an invalid file extension, its not showing me the error message. I am aware of solution to write custom validation attribute, but I dont want to use custom attribute. Please point out where I am going wrong.
I used the code above in FileExtensions attribute of DataAnnotations not working in MVC, I just did a couple of changes to: 1)avoid namespace collisions and 2) to use IFormFile instead of the original HttpPostedFileBase. Well, maybe is usefull to someone.
My code:
I had the same problem and I resolved creating a new ValidationAttribute.
Like this:
Now, just use this:
I have helped. Hugs!
The FileExtensions Attribute does not know how to verify a HttpPostedFileBase. Please try below
In your controller:
Like marai answered, the FileExtension Attribute only works on string properties.
In my code, i use the attribute as follows:
Then, in server side, ModelState.IsValid will be false if the postedfile doesn't have the entensions that you specify in the attribute (.zip and .pdf in my example).
Note: If you are using the
HTML.ValidationMessageFor
helper to render the error message after PostBack (The File Extension Attribute does not validate on client side, only server side), you need to specify another helper for theFileName
property in order to display the extension error message: