在数据库实体框架中获取并保存图像

时间:2019-04-30 19:02:40

标签: c# database asp.net-core entity-framework-core

我需要在注册用户时要求他上传照片并保存,然后拍摄

这是注册方法。

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 };
            var _result = await _userManager.CreateAsync(user, model.Password);
            if (_result.Succeeded)
            {

                await _signInManager.SignInAsync(user, true);
                //IdentityResult result = await _userManager.AddToRoleAsync(user, "User");
                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; }

查看

<form method="post" asp-controller="Account" asp-action="Register">
    <div asp-validation-summary="ModelOnly"></div>
    <div class="form-group">
        <label asp-for="Email"></label><br />
        <input asp-for="Email" placeholder="Email" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="FirstName"></label><br />
        <input asp-for="FirstName" placeholder="Имя" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="LastName"></label><br />
        <input asp-for="LastName" placeholder="Фамалия" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="Password"></label><br />
        <input asp-for="Password" placeholder="Пароль" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="PasswordConfirm"></label><br />
        <input asp-for="PasswordConfirm" placeholder="Подтвердите пароль" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="Address"></label><br />
        <input asp-for="Address" placeholder="Адрес" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="Avatar"></label><br />
        <input asp-for="Avatar" type="file" />
    </div>
    <button type="submit" class="btn btn-success">Зарегистрироваться</button>
</form>

如何在控制器中获取图像并将其写入数据库,如何获取?

0 个答案:

没有答案