Merge pull request #205 from m3rlinez/master
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / HttpPostedFileBaseModelBinder.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc {\r
14     using System;\r
15     using System.Web;\r
16 \r
17     public class HttpPostedFileBaseModelBinder : IModelBinder {\r
18 \r
19         public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {\r
20             if (controllerContext == null) {\r
21                 throw new ArgumentNullException("controllerContext");\r
22             }\r
23             if (bindingContext == null) {\r
24                 throw new ArgumentNullException("bindingContext");\r
25             }\r
26 \r
27             HttpPostedFileBase theFile = controllerContext.HttpContext.Request.Files[bindingContext.ModelName];\r
28 \r
29             // case 1: there was no <input type="file" ... /> element in the post\r
30             if (theFile == null) {\r
31                 return null;\r
32             }\r
33 \r
34             // case 2: there was an <input type="file" ... /> element in the post, but it was left blank\r
35             if (theFile.ContentLength == 0 && String.IsNullOrEmpty(theFile.FileName)) {\r
36                 return null;\r
37             }\r
38 \r
39             // case 3: the file was posted\r
40             return theFile;\r
41         }\r
42 \r
43     }\r
44 }\r