我在这个
的aspx页面上有一个图像控件 <asp:Image ID="Image1" runat="server" Height="64px" Width="64px"
ImageUrl='<%# "SideImageHandler.ashx?ID=" + Eval("ID")%>'/>
我的imagehandler代码看起来像这样
public void ProcessRequest(HttpContext context)
{
SqlConnection con = new SqlConnection();
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["GalleryConnectionString"].ConnectionString;
// Create SQL Command
Utility.ImageID = 2;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT IMAGE FROM Icon WHERE (ID ="+ Utility.ImageID+")";
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
SqlParameter ImageID = new SqlParameter("@ID", System.Data.SqlDbType.Int);
ImageID.Value = context.Request.QueryString["ID"];
cmd.Parameters.Add(ImageID);
con.Open();
SqlDataReader dReader = cmd.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["IMAGE"]);
dReader.Close();
con.Close();
}
但它并没有向我显示图像。它出了什么问题?
另外,我有一个下载按钮,当用户点击它时图像将被下载我是新的不知道我在下载按钮点击事件上放了什么代码?请提前感谢指导我
答案 0 :(得分:3)
这只是示例。 Use:
<asp:image id="Image1" imageUrl="SideImageHandler.ashx?ID=<someId>"/>
在config中添加:
<httpHandlers>
<add verb="*" path="img/*" type="SideImageHandler"/>
</httpHandlers>
并在处理程序中:
public void ProcessRequest (HttpContext context)
{
int ID;
if (context.Request.QueryString["ID"] != null)
ID= Convert.ToInt32(context.Request.QueryString["ID"]);
else
throw new ArgumentException("No parameter specified");
byte[] imageData= ;//get the image data from the database using the employeeId Querystring
Response.ContentType = "image/jpeg";
Response.BinaryWrite(imageData);
}