Senura Dissanayake
Senura Dissanayake

Reputation: 663

ASP.net Image retrieved from database not show in Chrome and Firefox except IE and Edge

I have created a food ordering web page. I have saved the images in database as varbinary. It correctly displays on IE and Edge. But not in Firefox and Chrome. (I am seeing a torn page icon instead where the image should be).

I have write this code in the ImageLoad.aspx.cs

public partial class ImageLoad : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ImageID"] != null)
        {
            string strQuery = "select Name, contentType, Data, description from food where fid=@id";
            String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["RestaurantDBConnectionString"].ConnectionString;
            SqlCommand cmd = new SqlCommand(strQuery);
            cmd.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(Request.QueryString["ImageID"]);
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            DataTable dt = new DataTable();
            cmd.Connection = con;
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
            if (dt != null)
            {


                try
                {
                    Byte[] bytes = (Byte[])dt.Rows[0]["Data"];
                    Response.Buffer = true;
                    Response.Charset = "";

                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.ContentType = dt.Rows[0]["ContentType"].ToString();
                    Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["Name"].ToString());
                    Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["description"].ToString());
                    Response.BinaryWrite(bytes);
                    Response.Flush();
                    Response.End();

                }

                catch (Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }
        }


    }
}

And included below code in the desserts.aspx page

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "False" Font-Names = "Arial" Caption = "Available Food" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black">
                <Columns>
                    <asp:BoundField DataField = "fID" HeaderText = "Item ID" ItemStyle-HorizontalAlign="Center" >
<ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:BoundField>
                    <asp:BoundField DataField="description" HeaderText="Name" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">

<HeaderStyle HorizontalAlign="Center"></HeaderStyle>

<ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:BoundField>

                    <asp:ImageField DataImageUrlField = "fID" DataImageUrlFormatString = "/ImageLoad.aspx?ImageID={0}" ControlStyle-Width = "200" ControlStyle-Height = "200" HeaderText = "Preview">
<ControlStyle Height="150px" Width="150px"></ControlStyle>
                    </asp:ImageField>
                </Columns> 
                  <FooterStyle BackColor="#CCCCCC" />
                  <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" HorizontalAlign="Center"/>
                  <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Center" />
                  <RowStyle BackColor="White" />
                  <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                  <SortedAscendingCellStyle BackColor="#F1F1F1" />
                  <SortedAscendingHeaderStyle BackColor="#808080" />
                  <SortedDescendingCellStyle BackColor="#CAC9C9" />
                  <SortedDescendingHeaderStyle BackColor="#383838" />
             </asp:GridView>

Here is the preview.. I have attached the preview below. Image -for IE OK but Firefox cannot display that

Here is the db contents > DB content image

Upvotes: 1

Views: 624

Answers (1)

Senura Dissanayake
Senura Dissanayake

Reputation: 663

I don't know what it does .. but I have removed the second content-disposition named Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["description"].ToString());

Now Firefox shows the Images. Credit goes to @Mathew

Upvotes: 1

Related Questions