C# 图像处理程序图像未在Chrome和FF中显示
我正在使用Handler显示来自数据库的员工档案图像。图像在Internet Explorer中显示良好,但在Chrome和Firefox中显示不出来。 有什么问题吗 这是我的密码: aspx:C# 图像处理程序图像未在Chrome和FF中显示,c#,asp.net,C#,Asp.net,我正在使用Handler显示来自数据库的员工档案图像。图像在Internet Explorer中显示良好,但在Chrome和Firefox中显示不出来。 有什么问题吗 这是我的密码: aspx: <% img_profile.ImageUrl="~/Handler.ashx?empcd="+Session["empcd"].ToString() ; %> <asp:Image CssClass="img-rounded img-responsive profile" ru
<% 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]
提前感谢您的帮助
更新:
<% 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)
{
}
}
我在我的aspx页面中添加了以下代码,现在显示image person.png,这意味着有任何错误。我如何找到并解决此错误
<% img_profile.ImageUrl="~/Handler.ashx?empcd="+Session["empcd"].ToString() ;
img_profile.Attributes["onerror"] = "this.src='Images/person.png';";
%>
您应该将其转换为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)
{
}
}
您没有指定
ContentType
。添加内容长度
是很好的做法
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注入的攻击,因为您不需要
验证empcd
,则不使用参数化查询
我应该如何以及在哪里调用此方法?只是修改了答案。更改位置:context.Response.BinaryWrite((字节[])rdr[“img”]);你每个人都有很多记录?还是一张唱片?我在答案上又做了一些修改。也许你再检查一次,这是不是最新的。显然,每个员工只有一条记录,记录在这里并不重要,因为它在Internet Explorer中工作正常,而图像可能存储不正确。您可以通过使用磁盘上的图像更改数据库中的图像并发送来测试这一点。如果图像中存在问题,为什么在IE中显示它?可能是因为IE不符合某些标准以及其他浏览器。以Base64格式发送图像也不起作用。您需要修复原始问题,而不是添加修复来显示标准图像。正如我前面提到的,尝试从磁盘发送映像。实际上我在一家公司工作,由于一些限制,我无法更新表中的映像:(