1 /* ****************************************************************************
\r
3 * Copyright (c) Microsoft Corporation. All rights reserved.
\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
9 * You must not remove this notice, or any other, from this software.
\r
11 * ***************************************************************************/
\r
13 namespace System.Web.Mvc {
\r
17 public class HttpPostedFileBaseModelBinder : IModelBinder {
\r
19 public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
\r
20 if (controllerContext == null) {
\r
21 throw new ArgumentNullException("controllerContext");
\r
23 if (bindingContext == null) {
\r
24 throw new ArgumentNullException("bindingContext");
\r
27 HttpPostedFileBase theFile = controllerContext.HttpContext.Request.Files[bindingContext.ModelName];
\r
28 return ChooseFileOrNull(theFile);
\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
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
43 // case 3: the file was posted
\r