Home > Enterprise >  How to store Picture User Profile on Database Table AspNetUsers using mvc asp.net core 5?
How to store Picture User Profile on Database Table AspNetUsers using mvc asp.net core 5?

Time:12-05

I working on asp.net core 5 mvc web application , I face issue I can't store Picture User on database table [dbo].[AspNetUsers].

so can you tell me how to store personal picture of user on table [dbo].[AspNetUsers].

I already add new column PictureUser on table [dbo].[AspNetUsers] to store User Picture .

I need to know what I will modify on controller and view to store user picture on database table .

I working on Microsoft identity membership when register user

my model as

public class RegisterVM
{
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public byte[] PictureUser { get; set; }

}

my action controller

    [HttpPost]
    public async Task<IActionResult> Register(RegisterVM registerVM)
    {
       
        var newUser = new ApplicationUser()
        {
            FullName = registerVM.FullName,
            Email = registerVM.EmailAddress,
            UserName = registerVM.EmailAddress,
            //How to add Image upload Here
        };
    
        var newUserResponse = await _userManager.CreateAsync(newUser, registerVM.Password);
        if (newUserResponse.Succeeded)
        {
            await _signInManager.SignInAsync(newUser, isPersistent: false);
      
            return View("RegisterCompleted");
        }
       
        return View(registerVM); 
    }

on view

@model RegisterVM;


        
                <form asp-action="Register">
                    <div asp-validation-summary="ModelOnly" ></div>

                    
                    <div >
                        <label asp-for="EmailAddress" ></label>
                        <input asp-for="EmailAddress"  />
                        <span asp-validation-for="EmailAddress" ></span>
                    </div>
                    <div >
                        <label asp-for="Password" ></label>
                        <input asp-for="Password"  />
                        <span asp-validation-for="Password" ></span>
                    </div>
                     //How to add Image upload Here

                    <div >
                        <input  type="submit" value="Sign up" />
                       
                    </div>
                </form>
            

How to add Image Upload on View And Controller Action this is Exactly my Question ?

CodePudding user response:

Inherit from the identityUser class and add the field related to the image and It is better to save the address of the image

Public class user : identityuser{

//add image property

Public string imgUrl {set; get;}

}

CodePudding user response:

public ActionResult GetImage(int id) {

// fetch image data or address from database

return File(imageData, "image/jpg");

}

<img src="@Url.Action("GetImage", new { id = 1 })" />

  • Related