Show image saved in database

1

I save my image in the sql server database, but I do not know how I can show it in another view in asp.net core.

the image that I keep, I do it in the following way:

    public async Task<IActionResult> Registro(UsuarioRegister user, List<IFormFile> Foto)
            {
                if (ModelState.IsValid)
                {
                    if (Foto.Count == 1)
                    {
                        foreach (var item in Foto)
                        {
                            if (item.Length > 0)
                            {
                                using (var stream = new MemoryStream())
                                {
                                    await item.CopyToAsync(stream);
                                    user.Foto = stream.ToArray();
                                }
                            }
                        }
                    }

                var usuario = new Usuario
                {

                    Foto = user.Foto
                };
                 _context.SaveChanges();
}

 public class UsuarioRegister
    {
        public byte[] Foto { get; set; }

    }

in the database, I keep the photo with the following type of data

foto varbinary(max) null

How can I show this saved photo, in another view?

    
asked by Karla Huerta 12.03.2018 в 05:59
source

1 answer

0

You could return the image in base64.

string imageBase64 = Convert.ToBase64String(Model.Foto);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);

And set that imageSrc in the src of your image.

    
answered by 12.03.2018 в 12:17