Asp.net mvc 输入不是有效的Base-64。我正在使用Asp.net mvc5
您好,我正在尝试将图像上载到AspNetUser的表。当用户注册时,我实现了所有功能,并且我可以选择图像,但当我提交表单时,我收到以下错误: System.FormatException:输入不是有效的Base-64字符串,因为它包含非Base-64字符、两个以上的填充字符或填充字符中的非法字符 我不知道怎么了。请帮忙。 这是我的密码 我在identitymodel中添加了这个新字段Asp.net mvc 输入不是有效的Base-64。我正在使用Asp.net mvc5,asp.net-mvc,razor,asp.net-identity,Asp.net Mvc,Razor,Asp.net Identity,您好,我正在尝试将图像上载到AspNetUser的表。当用户注册时,我实现了所有功能,并且我可以选择图像,但当我提交表单时,我收到以下错误: System.FormatException:输入不是有效的Base-64字符串,因为它包含非Base-64字符、两个以上的填充字符或填充字符中的非法字符 我不知道怎么了。请帮忙。 这是我的密码 我在identitymodel中添加了这个新字段 public string Region { get; set; } public string Us
public string Region { get; set; }
public string UserBio { get; set; }
public byte[] ProfilePicture { get; set; }
public string WebUrl { get; set; }
public string CompanyName { get; set; }
注册服务模型
public class RegisterViewModel
{
[Display(Name = "Profile Picture")]
public byte[] ProfilePicture { get; set; }
[Display(Name = "Your Website Url")]
public string WebUrl { get; set; }
[Display(Name = "Your Company Name")]
public string CompanyName { get; set; }
}
<div class="form-group">
@Html.LabelFor(m => m.ProfilePicture, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.ProfilePicture, new { type = "file" })
@Html.ValidationMessage("CustomMessage", new { @class = "text-danger" })
</div>
</div>
扩展标识模型
public class ExtendedIdentityModels : RegisterViewModel
{
public HttpPostedFileBase UserProfilePicture { get; set; }
}
a计数控制器
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(ExtendedIdentityModels model)
{
if (ModelState.IsValid)
{
if (model.UserProfilePicture != null)
{
if (model.UserProfilePicture.ContentLength > (4 * 1024 * 1024))
{
ModelState.AddModelError("CustomError", "Image can not be lager than 4MB.");
return View();
}
if (!(model.UserProfilePicture.ContentType == "image/jpeg" || model.UserProfilePicture.ContentType == "image/gif"))
{
ModelState.AddModelError("CustomError", "Image must be in jpeg or gif format.");
}
}
byte[] data = new byte[model.UserProfilePicture.ContentLength];
model.UserProfilePicture.InputStream.Read(data, 0, model.UserProfilePicture.ContentLength);
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.Lastname, Gender = model.Gender, Country = model.Country, Region = model.Region, UserBio = model.UserBio, WebUrl = model.WebUrl, CompanyName = model.CompanyName, ProfilePicture = data };
var result = await UserManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
result = await UserManager.AddToRolesAsync(user.Id, model.RoleName);
await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
// For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
// Send an email with this link
string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
return RedirectToAction("AccountEmailConfirmation", "Account");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
[HttpPost]
[异名]
[ValidateAntiForgeryToken]
公共异步任务);
返回重定向到操作(“AccountEmailConfirmation”、“Account”);
}
加法器(结果);
}
//如果我们走到这一步,有些东西失败了,重新显示形式
返回视图(模型);
}
Register.cshtml
public class RegisterViewModel
{
[Display(Name = "Profile Picture")]
public byte[] ProfilePicture { get; set; }
[Display(Name = "Your Website Url")]
public string WebUrl { get; set; }
[Display(Name = "Your Company Name")]
public string CompanyName { get; set; }
}
<div class="form-group">
@Html.LabelFor(m => m.ProfilePicture, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.ProfilePicture, new { type = "file" })
@Html.ValidationMessage("CustomMessage", new { @class = "text-danger" })
</div>
</div>
@LabelFor(m=>m.ProfilePicture,新的{@class=“col-md-2控制标签”})
@Html.TextBoxFor(m=>m.ProfilePicture,新的{type=“file”})
@Html.ValidationMessage(“CustomMessage”,new{@class=“text danger”})
一个
绑定到的属性是HttpPostedFileBase
,而不是byte[]
还有其他方法吗?如果有,请帮助,这是我第一次使用asp.net mvc 5No。您正在编辑数据。因此,请始终使用视图模型(请参阅),该视图模型将包含一个HttpPostedFileBase文件
属性