gpt4 book ai didi

c# - 我想限制文件上传只接受 .jpg 和 .png 文件,并限制文件大小

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:02 25 4
gpt4 key购买 nike

<分区>

我想限制个人资料图片上的文件大小和文件类型。我只想允许 .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);
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com