0

I have image byte array stored in session.

    Byte[] bytes = (Byte[])Session["STORED_IMAGE"];

I want display this in image control after postback. I have tried this code

    Byte[] bytes = (Byte[])Session["STORED_IMAGE"];
    Response.Buffer = true;
    Response.Charset = "";
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "image/jpeg";
    Response.AddHeader("content-disposition", "attachment;filename=sandra");
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();

It displays the image. but also downloads it. I just want to display not download. Can anyone help me doing this?Thanks in advance.

Shanna
  • 753
  • 4
  • 14
  • 34

2 Answers2

2

Just remove that line

 Response.AddHeader("content-disposition", "attachment;filename=sandra");

This line gives the "command" and browser starts to download it.

Update

If I understand well, you try to show this image on a certain place on the page. But I also understand that you add that code on the same page with the rest code. This can not work because you "break" the page.

Make a handler .ashx and there place that code.

Then from your page, call that handler as

<img src="showimage.ashx" />

and there you see it.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • I have tried with that. Then other contents disappears and only the image appears – Shanna Dec 18 '13 at 06:52
  • 1
    @Sandra If you have add this code on post back and overwrite the page flow, then you have other issues... Make a handler and show your image called from the handler – Aristos Dec 18 '13 at 06:54
  • I had tried with this handler public void ProcessRequest(HttpContext context) { var imageData = context.Session["STORED_IMAGE"] as byte[]; context.Response.OutputStream.Write(imageData, 0, imageData.Length); context.Response.ContentType = "image/JPEG"; } public bool IsReusable { get { return false; } } – Shanna Dec 18 '13 at 06:56
  • If I add it in Handler, the image does not display – Shanna Dec 18 '13 at 07:03
  • @Sandra Do you see what error you get there ? Probably you do not have add the session on the handler. I say that because you use session. Open the results from the handler or debug it to see your errors ... read this: http://stackoverflow.com/questions/14181408/httpcontext-current-session-is-null-in-ashx-file/14181556#14181556 – Aristos Dec 18 '13 at 07:05
  • @Sandra Well, probably the session is not have the time to write that data, or I do not know...., you need to design it show other way to make it work. Your design is the main issue. – Aristos Dec 18 '13 at 07:08
1

Add handler

with parameter

 <asp:Image runat="server" Width="40px" Height="40px" ImageUrl='<%# "Handler.ashx?VehicleCode=" + Eval("VehicleCode")%>'>

without parameter

 <asp:Image runat="server" Width="40px" Height="40px" ImageUrl='<%# "Handler.ashx %>'>

inside handler get image from db and pass as this

 public void ProcessRequest(HttpContext context)
    {

      //if you pass parameter use this
        string para = context.Request.QueryString["VehicleCode"];

      //get the image from data base in here im using a web service
        System.Data.DataSet ds = new System.Data.DataSet();
        MMS_MasterWebService.MMS_MasterMaintenance obj = new MMS_MasterWebService.MMS_MasterMaintenance();
        obj.Url =  "http://192.168.48.10/SHOREVision_MMS_Service/MMS_MasterMaintenance.asmx";
        ds = obj.GetVehicleMasterByCode(para);
        context.Response.BinaryWrite((byte[])ds.Tables[0].Rows[0][21]);

    }
Amila Thennakoon
  • 419
  • 1
  • 5
  • 15