C# Can';t通过图像处理程序在图像控件中检索图像
我成功地将图像存储在数据库中,但我无法检索它 我有一个数据库C# Can';t通过图像处理程序在图像控件中检索图像,c#,asp.net,image,handler,C#,Asp.net,Image,Handler,我成功地将图像存储在数据库中,但我无法检索它 我有一个数据库ImageDatabase,有一个表Pict,其中包含两列Letter(varchar(50))(pk)和Picture(image) 我的Handler.ashxcode: using System; using System.Web; using System.Data.SqlClient; using System.IO; public class Handler : IHttpHandler { public void Pr
ImageDatabase
,有一个表Pict
,其中包含两列Letter
(varchar(50)
)(pk)和Picture
(image
)
我的Handler.ashx
code:
using System;
using System.Web;
using System.Data.SqlClient;
using System.IO;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string L = (context.Request.QueryString["Letter"]);
SqlConnection connection = new SqlConnection();
connection.ConnectionString = @"Data Source=RANJHANI-PC\SQLEXPRESS;Initial Catalog=ImageDatabase;Integrated Security=True";
string sql = "SELECT Picture FROM Pict WHERE Letter = @Ltr";
SqlCommand cmd = new SqlCommand(sql, connection);
cmd.Parameters.AddWithValue("@Ltr", L);
connection.Open();
byte[] bytes = (byte[]) cmd.ExecuteScalar();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(bytes);
connection.Close();
}
public bool IsReusable {
get {
return false;
}
}
}
我的默认值.aspx
<img id="img1" src="Handler.ashx?Letter='a'" />
请在这方面帮助我,如果我犯了错误,请在写入二进制文件之前尝试清除响应流
context.Response.Clear();
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(bytes);
context.Response.End();
当在浏览器中请求
Handler.ashx?Letter='a'
时,您会得到什么?无兄弟,我的图像未加载,图像控件上有一个红十字图标。请使用firebug或chrome developer工具检查结果,你是否得到了200 OK或500个内部错误,它说加载失败,给定URL仍然收到相同的问题在context.Response.BinaryWrite(字节)上设置断点编码>并检查字节长度