我需要从视图和控制器中获取照片,但总是空的
我需要从视图和控制器中获取照片,但是它始终为空。我尝试下载jpeg。
HTML
<div class="form-group">
<label asp-for="Avatar"></label><br />
<input asp-for="Avatar" multiple>
</div>
控制器
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
User user = new User { Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, UserName = model.Email, Address = model.Address };
using (var memoryStream = new MemoryStream())
{
await model.Avatar.CopyToAsync(memoryStream);
user.Avatar = memoryStream.ToArray();
}
var _result = await _userManager.CreateAsync(user, model.Password);
if (_result.Succeeded)
{
await _signInManager.SignInAsync(user, true);
//IdentityResult result = await _userManager.AddToRoleAsync(user, "User");
await _userRepository.SendConfirmEmailMessage(user.Email);
return RedirectToAction("Index", "Home");
}
else
{
foreach (var error in _result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
}
return View(model);
}
RegisterViewModel
public IFormFile Avatar { get; set; }
用户
public byte[] Avatar { get; set; }
答案 0 :(得分:0)
尝试一下。
RegisterViewModel
[FromForm]
public IFormFile Avatar { get; set; }
并删除ApiController
属性