I have a controller that handles file uploads. Ultimately I would like to be able to create attribute to decorate my controller actions like [HttpPostedFileType("zip")]
or something similar.
Currently I created this extension method which I use in the action.
public static string GetFileExtension(this HttpPostedFileBase file)
{
if (!file.FileName.Contains('.'))
throw new FormatException("filename does not contain extension");
return file.FileName.Split(".".ToCharArray()).Last();
}
The action signature is
[HttpPost]
public ActionResult Shapefile(HttpPostedFileBase file)
{
file.GetFileExtension()
...
}
I started to create a HttpPostedFileTypeAttribute and was thinking I'd override the OnActionExecuting method and call the extension. In this case with posted files, I can get the Http request and loop over the files but with mvc's model binding having a HttpPostedFileBase or an enumeration of those is much cleaner than the asp 1.x way of getting to the files.
My question is, can I get the parameters in the attribute on action executing or have they not been bound yet since the life cycle hasn't hit the action method yet? Should I create a model with a HttpPostedFileBase property and create a validation attribute? Recommendations?