Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to rerieve image which is stored as blob in Oracle Database .I am using MVC and
ViewData to get the image from controller to View .

------------------- Model CODE IS HERE--------------
 public class SelectionModel   (This is the model class )
 {

 public byte[] ImageDsiplay
 {
      get;
      set;
 }

 public MemoryStream ImageStream
 {
      get;
      set;
 }
}


----------Controller Code IS HERE-----
   public  ActionResult GetImageInfo(SelectionModel  sel) (This is the controller method)
{
    byte[]  imagedata ;
    string sql = "select filecontent from filestore where rownum=1 ";

    OracleConnection con = new OracleConnection(ConStr);
    OracleCommand com = new OracleCommand(sql, con);

    try
    {
        con.Open();
        OracleDataReader dr;
        dr = com.ExecuteReader();

        while (dr.Read())
        {
            imagedata = (byte[])dr[0];
            sel.ImageDsiplay = imagedata;
            var stream = new MemoryStream(sel.ImageDsiplay);
            sel.ImageStream = stream;
        }
    }

    catch (Exception ex)
    {
    }

   return  View(sel);
  }


-----Here is the view which gets the image from the controller---------
<img src="@ViewData["image"]", width="500px" alt="Alternate Text" />



Now My question is: Am I missing anything? Because I can't see the image in my View. Is thre anything wrong with the image Tag? I might be missing something. I have also tried:
HTML
SRC="DATA:IMAGEdata:image/gif;base64
Any solution?
Posted
Updated 17-Oct-13 5:53am
v3

1 solution

You can use retrun file, for returning an Image file:
C#
return File(sel.ImageStream , "image/png");

and on your view you can call your action result
XML
<img style="width:260px" src="@Url.Action("ActionResult", "Controller")" alt="myimage" />

I Hope it help you,

But about your code, didn't you assign viewdata collection in your controller?,it doesn't work if you don't assign any value to it, and also i think it is only for simple data types, and not for blob data.
 
Share this answer
 
v2
Comments
Member 10342533 17-Oct-13 17:08pm    
Thank You Mehdy Moini . It worked ..About my Code I am sory for some reason my view data collection in the controller is missed . Originally I had that ViewData["image"] = sel.ImageStream; Anyways As you said it wouldnt be blob datatypes.Thank you so much
Mehdy Moini 18-Oct-13 4:05am    
Your welcome.

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