2016-12-22 39 views
1

我創建了一個食品訂購網頁。我已將圖像作爲varbinary保存在數據庫中。它在IE和Edge上正確顯示。但不適用於Firefox和Chrome。 (我看到一個殘破的頁面圖標,而不是圖像應該在的位置)。ASP.net從數據庫中檢索的圖像不能在Chrome和Firefox中顯示,除IE和Edge外

我有寫在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=[email protected]"; 
      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()); 
       } 
      } 
     } 


    } 
} 

這個代碼,並在頁面desserts.aspx

<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> 

這裏包括下面的代碼預覽。我已附上以下預覽。 Image -for IE OK but Firefox cannot display that

這裏是DB內容>DB content image

+0

你能分享圖像名稱與保存在您的數據庫中的擴展名? –

+0

是的,圖像名稱是(有幾個圖像) - blueberry-lemon-napoleon-8.jpg和擴展名爲.jpg – Senura

+0

名爲Food的表具有name和contentType字段。 – Senura

回答

1

我不知道它做什麼..但我已刪除了名爲Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["description"].ToString());

第二content-disposition現在火狐顯示形象。信貸去@Mathew

+0

我已經理解了..當我們刪除所有的內容處置時,它不會直接訪問那個圖片url時下載。它只能觀看它! – Senura

相關問題