Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc2 / 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             return ChooseFileOrNull(theFile);\r
29         }\r
30 \r
31         // helper that returns the original file if there was content uploaded, null if empty\r
32         internal static HttpPostedFileBase ChooseFileOrNull(HttpPostedFileBase rawFile) {\r
33             // case 1: there was no <input type="file" ... /> element in the post\r
34             if (rawFile == null) {\r
35                 return null;\r
36             }\r
37 \r
38             // case 2: there was an <input type="file" ... /> element in the post, but it was left blank\r
39             if (rawFile.ContentLength == 0 && String.IsNullOrEmpty(rawFile.FileName)) {\r
40                 return null;\r
41             }\r
42 \r
43             // case 3: the file was posted\r
44             return rawFile;\r
45         }\r
46 \r
47     }\r
48 }\r