Hi, I need to upload a pdf file on server so I am using
<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
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
[ValidateFile(ErrorMessage = "You must upload file before you proceed.")]
public HttpPostedFileBase File { get; set; }
in view
<input type="file" name="InvoiceFile" id="FileField1" size="38" />
@Html.ValidationMessageFor(x => x.File)
in controller
if (!ModelState.IsValid)
{
return RedirectToAction("Work_orders", "Contractor");
}
while I debug the code I get null in object value parameter of
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