hello Am trying to Upload an image to AspNetUser's table when a user register, i implemented everything and i can select an image but when i submit the form i get this Error:
System.FormatException: The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
I don't know what's wrong. Please help. this is my code
i added this new fields in the identitymodel
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; }
RegisterViewModel
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; }
}
ExtendedIdentityModels
public class ExtendedIdentityModels : RegisterViewModel
{
public HttpPostedFileBase UserProfilePicture { get; set; }
}
AcountController
[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);
}
Register.cshtml
<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>