LA.NET [EN]

Mar 17

Today we’re going to keep looking at the futures assembly. In a previous post, we’ve seen how we can easily return a file from an action method. Today, we’re going to take a look at how we can use the FileCollectionModelBinder to get access to a group of files that have been uploaded to the server. The first thing you need to do when you want to use this binder is to register it. The FileCollectionModelBinder introduces a static method that registers the binder for all types of collections off HttpPostFileBase items. We can do this from within application start event:

protected void Application_Start() {
   RegisterRoutes(RouteTable.Routes);
   FileCollectionModelBinder.RegisterBinder( ModelBinders.Binders );
}

After doing this, all parameters of type HttpPostedFileBase[], IEnumerable<HttpPostedFileBase>, ICollection<HttpPostedFileBase>, IList<HttpPostedFileBase>, Collection<HttpPostedFileBase> and List<HttpPostedFileBase> will be automatically filled by this custom binder. In order to illustrate its use, we’ll start by adding a form to a view that lets you update files. Here’s the markup we’re using:

<% using( var form = Html.BeginForm(
                                         "ProcessFiles",
                                         "Home",
                                         FormMethod.Post,
                                         new { enctype = "multipart/form-data"}))
{%>
    <input type="file" name="files" />
    <input type="file" name="files" />
    <input type="file" name="files" />
    <input type="file" name="files" />
    <input type="submit" value="Send files" />
<% }%>

As you can see,we need to add the enctype attribute “by hand” to the form tag. If you don’t do that,you’ll end up without no files on the server side. Another interesting thing is that we’re defining several <input type=”file”> elements and we’re giving them the same name. We’re doing this because we want to get them all on the server side. Here’s the code for the ProcessFiles method:

public ActionResult ProcessFiles(IEnumerable<HttpPostedFileBase> files) {
  //do something with the files here
  return GetView();
}

If everything works out, files will give a collection with all the files submitted by the user on the browser. It’s important to keep in mind the rules associated with the binding associations: the parameter name must match the name of the files you want to retrieve form the form (in other words, make sure that the parameter name matches the name attribute you’ve passed to the input elements of type file you want to recover). I haven’t tested it, but this should also work with custom objects with properties that match one of the types handled by this binder.

And that’s all for today. Keep tuned for more on MVC.

2 comments so far

  1. Ahmad Lafi
    6:47 am - 12-3-2009

    Thanks man. I”ve been searching for this for more than a month!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>