2

I am currently trying to validate a file that will be uploaded onto a page. I am wanting to be able to validate that the file type is a .doc,.docx,.txt,.pdf,.rtf and that the size is 4 mb or less. I tried to use regular expression to validate the type with the following code.

     [RegularExpression(@"(^.*\.(rtf|doc|docx|pdf)$", ErrorMessage =@"Please upload a (.pdf, .doc, .docx, .txt, or .rtf) file")]

I then tried to validate the file size and type by creating the following class

     public class ValidateFileAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        int MaxContentLength = 1024;
        string[] AllowedFileExtensions = new string[] { ".doc", ".docx", ".pdf", ".txt", ".rtf" };

        var file = value as HttpPostedFileBase;

        if (file == null)
            return false;

        else if (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf('.'))))
        {
            ErrorMessage = "Upload File Type: " + string.Join(", ", AllowedFileExtensions);
            return false;
        }

        else if (file.ContentLength > MaxContentLength)
        {
            ErrorMessage = "The Size of the file is too large : " + (MaxContentLength / 1024).ToString() + "MB";
            return false;
        }

        else
            return true;
    }

}

(I understand that the 1024 will not allow me to upload a very large file but for testing reasons I left it at that to see if it would work).

In the view I used @Html.TextBoxFor(m => m.CV.File, new { type="file"}) @Html.ValidationMessageFor(m => m.CV.File)

I am unsure of how to get the validation to work correctly, it won't allow me to input a different file type however, it doesn't recognize the correct file types either.

race155
  • 23
  • 7
  • 1
    Firstly file size and file type are 2 different things and you need 2 different validation attribute which would be used like `[FileSize(1024)]` and `[FileType("rtf|doc|docx|pdf")]`. For an implementation of a `FileSizeAttribute` attribute, refer [this question/answer](http://stackoverflow.com/questions/33431543/fileextension-validation-using-custom-validation-creates-duplicate-and-invalid-d/33433562#33433562) and for an example of a `FileTypeAttribute`, refer [this article](http://blog.2mas.xyz/creating-custom-unobtrusive-file-extension-validation-in-asp-net-mvc-3-and-jquery/). –  Jun 10 '16 at 03:28
  • Both example give both server side and client side validation. For client side validation you must implement `IClientValidatable` and add the rules to the `jQuery.validator`. For more information, refer to [this article](http://www.devtrends.co.uk/blog/the-complete-guide-to-validation-in-asp.net-mvc-3-part-2) –  Jun 10 '16 at 03:32
  • I referred to the FileType example but I ran into a couple of issues. The first was that in the `protected override Validation Result` it says it is not valid because it does not return if it does not go into the if statement. The second thing is that it is not going into the `public FileTypeAttribute(string validTypes)` it grabs the validtypes from above but does not actually validate anything. – race155 Jun 14 '16 at 19:36
  • As for the first issue - see the edit to the linked answer (needs `return ValidationResult.Success;` at the end). As for you 2nd issue, I don't understand your comment. –  Jun 15 '16 at 02:33
  • After looking more into the second issue I found out that when the page is loading the file box it is going through and doing the validation but when I go to submit the file it is just allowing all sizes and types into the next page. I'm not sure why this is happening. – race155 Jun 15 '16 at 17:27
  • You have not shown the code your using so I cannot comment (the code I linked to works correctly) –  Jun 16 '16 at 02:17

0 Answers0