Click here to Skip to main content
15,355,659 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I need to upload a pdf file on server so I am using
HTML
<input type="file" name="InvoiceFile" id="FileField1" size="38" />
for this. But somewhere I saw fileupload is not accessible through javascript for security reasons. Now only one option left (as I think) creating custom validation attribute. so I wrote following code in Model

C#
public class ValidateFileAttribute : RequiredAttribute
   {
       public override bool IsValid(object value)
       {

               var file = value as string;
               if (file == null)
               {
                   return false;
               }
               else
               {
                   return true;
               }

       }
   }


and in model calss

C#
[ValidateFile(ErrorMessage = "You must upload file before you proceed.")]
        public HttpPostedFileBase File { get; set; }


in view

HTML
<input type="file" name="InvoiceFile" id="FileField1" size="38" />
                                        @Html.ValidationMessageFor(x => x.File)


in controller

C#
 if (!ModelState.IsValid)
                {
                    return RedirectToAction("Work_orders", "Contractor");
                }
// rest of code


while I debug the code I get null in object value parameter of
C#
public override bool IsValid(object value)
of custom attribute class even after uploading file. so its always returning false and I am unable to pass validations, any solution.
Thanks
Posted
Comments
♥…ЯҠ…♥ 10-Dec-13 2:48am
   
What type of validation you want to implement for Input type? Just to validate file exists?
[no name] 10-Dec-13 2:50am
   
yes, thats enough

1 solution

Hi Imadoddin,

I think you can find solution here[^].
Moreover you can refer this link [^]and this one[^].
You can use Javasceript to do it.

Hope this helps you a bit.

Regards,
RK
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900