2016-11-30 59 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中显示? –