I want to restrict the file size and file type on the profile picture. I want to allow only .jpg and .png pictures, and i also want to only allow a maximum file size of for example 1 megabyte. Under you see my code for uploading a file with no restrictions. I am using base64. I need to check file type and file size before the picture is uploaded but I really don't know how and where. If you need to see more of my code please let me know. Thank you very much.
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> ChangePic(IndexViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByIdAsync(User.GetUserId());
var breader = new BinaryReader(model.ProfilePic.OpenReadStream());
var byteImage = breader.ReadBytes((int)breader.BaseStream.Length);
user.ProfilePic = byteImage;
var result = await _userManager.UpdateAsync(user);
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
//var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
//var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
//await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
// "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "Profile info updated");
return RedirectToAction(nameof(ManageController.Index), "Manage");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}