Click here to Skip to main content
15,888,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have saved a photo in database converting it to binary how can i retrieve and show it in view
Posted
Comments
[no name] 11-Dec-15 0:40am    
It is little bit long process to implement. Please go through below link:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129[^]
Anisuzzaman Sumon 11-Dec-15 1:39am    
What is your column dataType ?

Use following codes

C#
SqlConnection conn = new SqlConnection();
          conn.ConnectionString = @"Data Source=.\SQLEXPRESS;Database=LINQ_TestDB;User Id=sa;Password=Sa123;";
          conn.Open();
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = conn;
          cmd.CommandText = "readImageData";
          cmd.CommandType = CommandType.StoredProcedure;

          SqlDataReader reader= cmd.ExecuteReader();
          List<ImageData> lstImageData=new List<ImageData>();
          while (reader.Read())
          {
              ImageData imd=new ImageData();

              imd.FileName=reader[1].ToString();
              byte[] mybyte = (byte[])reader[2];
              imd.ImagePath ="data:application/jpeg;base64," +Convert.ToBase64String(mybyte);

              lstImageData.Add(imd);
          }

          return View(lstImageData);



And View:
HTML
@using (Html.BeginForm("ImageUpload", "Image", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    Html.Label("Select Image:");
    <input type="file" name="my_image"/>
    <input type="submit" name="submit" value="Upload" />

}
@foreach (ImageData imd in Model)
{ 
<h3>@imd.FileName</h3>
<img src="@imd.ImagePath" Width="200" height="250" />
}



Model Class
C#
public class ImageData
   {
       public int ID { get; set; }
       public string FileName { get; set; }
       public byte[] FileByte { get; set; }
       public string ImagePath { get; set; }
   }


I Guess your column type is varbinary(MAX)
 
Share this answer
 
v3
Comments
Member 11970398 11-Dec-15 3:54am    
what is the model class for this
Member 11970398 11-Dec-15 3:59am    
where i will pass the id so that it will take image for that id only
Member 11970398 11-Dec-15 4:02am    
which column it show where i will pass that column name
Anisuzzaman Sumon 11-Dec-15 4:50am    
just modify it and feel free to ask question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900