forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteArrayModelBinder.cs
More file actions
37 lines (31 loc) · 1.34 KB
/
Copy pathByteArrayModelBinder.cs
File metadata and controls
37 lines (31 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace System.Web.Mvc
{
public class ByteArrayModelBinder : IModelBinder
{
public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException("bindingContext");
}
ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
// case 1: there was no <input ... /> element containing this data
if (valueResult == null)
{
return null;
}
string value = valueResult.AttemptedValue;
// case 2: there was an <input ... /> element but it was left blank
if (String.IsNullOrEmpty(value))
{
return null;
}
// Future proofing. If the byte array is actually an instance of System.Data.Linq.Binary
// then we need to remove these quotes put in place by the ToString() method.
string realValue = value.Replace("\"", String.Empty);
return Convert.FromBase64String(realValue);
}
}
}