プロファイル画像のファイルサイズとファイルタイプを制限します。私は.jpgと.pngピクチャだけを許可したいと思います。また、最大ファイルサイズ(例えば1メガバイト)を許可したいとします。あなたの下では、制限なしでファイルをアップロードするための自分のコードを参照してください。私はbase64を使用しています。私は画像がアップロードされる前に、ファイルの種類とファイルサイズを確認する必要がありますが、私は実際にどのように、どこにいるのかわかりません。私のコードをもっと見る必要があるなら、私に知らせてください。どうもありがとうございました。ファイルアップロードを制限して.jpgファイルと.pngファイルのみを許可し、ファイルサイズを制限する
[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);
}
検証属性を使用すると、クライアント側とサーバー側の両方の検証を行うことができます。[この例のFileTypeAttribute](http://stackoverflow.com/questions/33414158/checking-image-mime-size-etc- in-mvc/33426397#33426397)(そして 'FileSizeAttribute'へのリンクを含みます) –