我想限制个人资料图片上的文件大小和文件类型。我只想允许 .jpg 和 .png 图片,而且我还想只允许最大文件大小,例如 1 兆字节。在您下面可以看到我的无限制上传文件的代码。我正在使用 base64。我需要在上传图片之前检查文件类型和文件大小,但我真的不知道如何以及在哪里。如果您需要查看我的更多代码,请告诉我。非常感谢。
[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);
}
我是一名优秀的程序员,十分优秀!