I have a form where a user can upload a file to the sites download section. However when the form is submitted I get this error, without the request ever making it to the ac
You could try removing the HttpPostedFileBase from the controller method and use Request.Files[0] and see if that makes a difference.
Honestly though I dont'see why this would be failing unless there is something that is causing it within your Model.
Also, nit picking here but DownloadFile should be downloadFile on your form and in your controller method.
Alex, you are partially correct with your assessment. The reason why it fails when you have a property of the same name on your model as the name of the input object on the form is due to the fact that the DataType of the matchingly-named property on your model is not System.Web.HttpPostedFileWrapper which is the datatype that the binary-binder will attempt to perform a bind to.
Excluding your property by using the Bind attribute and then extracting the file from the Request.Files collection as you demonstrated may work, but it may be more elegant to let the binder do its thing (provided that the data type is matching as I mentioned above) and then you can simply access the file directly from the property of your model
Ok I figured this out finally, It was all caused because I named the file input on the form the same as my models file field, so the model binder was picking this up and trying to bind the posted file directly to the binary property which was throwing an exception because the string was not binary.
So to fix it I simply added this to my create action method:
[HttpPost]
[Authorize]
public ActionResult Create([Bind(Exclude = "DownloadFile")] Download dl, HttpPostedFileBase DownloadFile)
{
By telling the model binder to exclude the field it solved the problem.
Thanks, Alex.
EDIT: This could also easily be solved by using view models