2016-11-30 120 views
0

我正在使用處理程序顯示來自數據庫的員工簡檔圖像。圖像在Internet Explorer中顯示效果良好,但未在Chrome和Firefox中顯示。 可能是什麼問題?在Chrome和FF中未顯示圖像處理程序圖像

這裏是我的代碼:

ASPX:

<% img_profile.ImageUrl="~/Handler.ashx?empcd="+Session["empcd"].ToString() ; 
    %> 
<asp:Image CssClass="img-rounded img-responsive profile" runat="server" ID="img_profile" Width="150" Height="150" /> 

圖像處理代碼:

public void ProcessRequest(HttpContext context) 
{ 
    try 
    { 
     OracleDataReader rdr = null; 
     OracleConnection dbConn; 
     dbConn = Conn.getConn(); 
     string empcd = context.Request.QueryString["empcd"].ToString(); 
     OracleCommand cmd = new OracleCommand("select photo img from olphrm.emp_personal where emp_code='"+empcd+"'", dbConn); 

     dbConn.Open(); 
     rdr = cmd.ExecuteReader(); 
     while (rdr.Read()) 
     { 

     context.Response.BinaryWrite((byte[])rdr["img"]); 
     } 
     if (rdr != null) 
     rdr.Close(); 
    } 
    catch (Exception ex) 
    { 

    } 
} 

這是在Chrome輸出:

[! [在這裏輸入圖像描述] [1]] [1]

在此先感謝您的幫助!

UPDATE:

我已經添加下面的代碼在我的aspx頁面,現在顯示的圖像person.png,這意味着有任何錯誤我。怎麼可以找到並解決這個問題?

<% img_profile.ImageUrl="~/Handler.ashx?empcd="+Session["empcd"].ToString() ; 

    img_profile.Attributes["onerror"] = "this.src='Images/person.png';"; 
                 %> 

回答

-1

您應該將其轉換爲base64字符串。

public void ProcessRequest(HttpContext context) 
{ 
    try 
    { 
     OracleDataReader rdr = null; 
     OracleConnection dbConn; 
     dbConn = Conn.getConn(); 
     string empcd = context.Request.QueryString["empcd"].ToString(); 
     OracleCommand cmd = new OracleCommand("select photo img from olphrm.emp_personal where emp_code='"+empcd+"'", dbConn); 

     dbConn.Open(); 
     rdr = cmd.ExecuteReader(); 
     while (rdr.Read()) 
     { 

     context.Response.Write(Convert.ToBase64String((byte[])rdr["img"])); 
     } 
     if (rdr != null) 
     rdr.Close(); 
    } 
    catch (Exception ex) 
    { 

    } 
} 
+0

如何以及應該在哪裏調用此方法? –

+0

只是修改了答案。 改爲: context.Response.BinaryWrite((byte [])rdr [「img」]); –

+0

我試過了,不工作 –

0

您沒有指定ContentType。並且加入content-Length是很好的做法。既然你不 驗證empcd,並沒有使用參數化查詢

public void ProcessRequest(HttpContext context) 
{ 
    //create a new byte array 
    byte[] bin = new byte[0]; 

    //get your data from the db 
    while (rdr.Read()) 
    { 
     bin = (byte[])rdr["img"]; 
    } 

    //clear the buffer stream 
    context.Response.ClearHeaders(); 
    context.Response.Clear(); 
    context.Response.Buffer = true; 

    //set the correct ContentType 
    context.Response.ContentType = "image/jpeg"; 

    //set the filename for the pdf 
    context.Response.AddHeader("Content-Disposition", "attachment; filename=\"myImage.jpg\""); 

    //set the correct length of the string being send 
    context.Response.AddHeader("content-Length", bin.Length.ToString()); 

    //send the byte array to the browser 
    context.Response.OutputStream.Write(bin, 0, bin.Length); 

    //cleanup 
    context.Response.Flush(); 
    context.ApplicationInstance.CompleteRequest(); 
} 

你的代碼是SQL注入非常脆弱。

+0

我試過了你的代碼,還是有同樣的問題;( –

+0

然後圖像可能沒有正確存儲,你可以通過從磁盤上的圖像改變圖像併發送它來測試。 – VDWWD

+0

如果有問題圖像,爲什麼它在IE中顯示? –