Image stored in the bank is not being displayed in View

2

I'm trying to display an image that is stored in the database. When the Photo field (of Image type) of the table is null, I display a default image that is stored in the directory. The problem is that the images coming from the database are not being displayed.

TheActionyouareexhibitingbydoingthetreatmentisbelow:

[HttpGet]publicFileContentResultShowFoto(int?id){byte[]fileBytes=null;stringfileType=null;try{varterapeuta=db.TERAPEUTAs.Find(id);//BuscaaimagemnoBancoif(terapeuta!=null){if(terapeuta.FOTO!=null){fileBytes=terapeuta.FOTO;//dotipobyte[]fileType=System.Drawing.Imaging.ImageFormat.Jpeg.ToString();//TipodaFotoreturnFile(fileBytes,fileType);}else{Imageimg=System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/Content/img/SemFoto.jpg"));
                        return File(imageToByteArray(img), System.Drawing.Imaging.ImageFormat.Jpeg.ToString());
                    }
                }
                else
                {
                    Image img = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("~/Content/img/SemFoto.jpg"));
                    return File(imageToByteArray(img), System.Drawing.Imaging.ImageFormat.Jpeg.ToString());
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

View's code for image display is below:

 @using (Html.BeginForm("Buscar", "Terapeutas", FormMethod.Post, htmlAttributes: new { @id = "form1", @name = "form1" }))
    {
        @Html.ValidationSummary(true)

        foreach (var item in Model.Terapeutas)
        {
            <img src="/Terapeutas/[email protected]_TERAPEUTA" class="img-circle" alt="Foto Terapeuta" width="120" height="120" />                }
        }
    }
    
asked by anonymous 06.11.2015 / 15:16

1 answer

1

I am answering my own question because I have been able to solve the problem. This will help others as well.

I've changed how to upload the image to the bank:

if (Request.Files.Count == 1)
                            {
                                var size = Request.Files[0].ContentLength;
                                var type = Request.Files[0].ContentType;
                                Stream fileStream = Request.Files[0].InputStream;

                                byte[] fotoBytes = new byte[fileStream.Length];
                                fileStream.Read(fotoBytes, 0, fotoBytes.Length);

                                objTerapeuta.FOTO = fotoBytes;
                            }
    
06.11.2015 / 17:04