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
16 public class ByteArrayModelBinder : IModelBinder {
\r
17 public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
\r
18 if (bindingContext == null) {
\r
19 throw new ArgumentNullException("bindingContext");
\r
22 ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
\r
24 // case 1: there was no <input ... /> element containing this data
\r
25 if (valueResult == null) {
\r
29 string value = valueResult.AttemptedValue;
\r
31 // case 2: there was an <input ... /> element but it was left blank
\r
32 if (String.IsNullOrEmpty(value)) {
\r
36 // Future proofing. If the byte array is actually an instance of System.Data.Linq.Binary
\r
37 // then we need to remove these quotes put in place by the ToString() method.
\r
38 string realValue = value.Replace("\"", String.Empty);
\r
39 return Convert.FromBase64String(realValue);
\r