I am trying to upload a file with ASP.NET MVC.
The following code work perfectly fine:
// Read in the image data.
byte[] binaryData = null;
HttpPostedFileBase uploadedFile = Request.Files["ImageFileName"];
if (uploadedFile != null &&
uploadedFile.ContentLength > 0)
{
binaryData = new byte[uploadedFile.ContentLength];
uploadedFile.InputStream.Read(binaryData,
0,
uploadedFile.ContentLength);
}
But what I am trying to do is use the new FileCollectionModelBinder
found in the futures assembly.
I've found these two blog posts here and here explaining what to do. I follow these instructions but havne't had any luck -> the file
object is always null
.
Here is my method.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include = "Subject, Content")]
Post post,
HttpPostedFileBase file)
{
UpdateModel(post);
...
}
Notice how i'm trying to upload a file AND upload some post information, to a Post object.
Can anyone make any suggestions?
For the record, I have wired up the ModelBinder in my global.asax.cs. I've also made sure the form is a post with the enctype added:-
<form method="post" enctype="multipart/form-data" action="/post/create">