我已经设法使用imagehandler从数据库中检索我的图像,但问题是,它只使用第一个图像检索,而不是在不同的数据上显示不同的图像。
所以这是我的图像处理程序代码
我对此行(string volunteerNames = imageRequest.QueryString["VolunteerName"];
)有疑问,因为它只显示System.Byte[]
而不是显示名称
<%@ WebHandler Language="C#" Class=".ShowImage" %>
using System;
using System.Web;
using System.Configuration;
using System.Data.SqlClient;
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest imageRequest = context.Request;
HttpResponse imageResponse = context.Response;
//string connStr = ConfigurationManager.ConnectionStrings["DBconnStr"].ConnectionString;
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Users\\User\\Desktop\\ProjectAV2.9\\ProjectA\\ProjectA\\App_Data\\ProjectA.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True";
string queryStr = "SELECT * FROM TVolunteer WHERE VolunteerName = @Name";
string bQueryStr = "SELECT * FROM BVolunteer WHERE VolunteerName = @Name";
string volunteerNames = imageRequest.QueryString["VolunteerName"];
SqlCommand bCMD = new SqlCommand(bQueryStr, conn);
SqlCommand cmd = new SqlCommand(queryStr, conn);
bCMD.Parameters.AddWithValue("@Name", volunteerNames);
cmd.Parameters.AddWithValue("@Name", volunteerNames);
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
string volunteerNRIC = dr["VolunteerNRIC"].ToString();
byte[] volunteerProfilePicture = (byte[])dr["VolunteerProfilePicture"];
string volunteerName = dr["VolunteerName"].ToString();
string volunteerContact = dr["VolunteerContact"].ToString();
string volunteerAddress = dr["VolunteerAddress"].ToString();
string volunteerEmail = dr["VolunteerEmail"].ToString();
imageResponse.BinaryWrite(volunteerProfilePicture);
}
dr.Close();
SqlDataReader bDR = bCMD.ExecuteReader();
if (bDR.HasRows)
{
bDR.Read();
byte[] volunteerProfilePicture = (byte[])bDR["VolunteerProfilePicture"];
string volunteerName = bDR["VolunteerName"].ToString();
string volunteerContact = bDR["VolunteerContact"].ToString();
string volunteerAddress = bDR["VolunteerAddress"].ToString();
string volunteerEmail = bDR["VolunteerEmail"].ToString();
imageResponse.BinaryWrite(volunteerProfilePicture);
bDR.Close();
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
这是我的DataList
<asp:DataList ID="dl_tvolunteer" runat="server" CellPadding="3"
ForeColor="#333333" RepeatColumns="3"
RepeatDirection="Horizontal" Width="375px" Height="193px" GridLines="Both">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "VolunteerProfilePicture","~/ShowImage.ashx?VolunteerName="+ Eval("VolunteerName") %>' />
<br />
<asp:Label ID="lbl_Name" runat="server" Text='<%# Bind("VolunteerName") %>'></asp:Label>
<br />
<asp:Label ID="lbl_Contact" runat="server" Text='<%# Bind("VolunteerContact") %>'></asp:Label>
<br />
<asp:Label ID="lbl_Address" runat="server" Text='<%# Bind("VolunteerAddress") %>'></asp:Label>
<br />
<asp:Label ID="lbl_Email" runat="server" Text='<%# Bind("VolunteerEmail") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
答案 0 :(得分:0)
你应该在你的图像处理程序中定义你的mime类型:
imageResponse.ContentType = "Image/Jpeg";
编辑:您似乎应该像这样更改图片列。那个获取您的志愿者名称并将其传递给您的查询字符串参数:
<asp:Image ID="Image1" runat="server" ImageUrl='<%#
DataBinder.Eval(Container.DataItem,
"VolunteerName",
"~/ShowImage.ashx?VolunteerName={0}")
%>'/>