数据库图像未显示asp.net

时间:2013-03-05 05:51:55

标签: asp.net

尝试在图像控件中显示数据库中的图像......第三天......到目前为止没有运气......

Employee.aspx上的Displaybutton

 Protected Sub DisplayButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DisplayButton.Click

    bind()
    GridView1.Visible = "True"
    If EmployeeIDTextBox.Text = "" Then
        MsgBox("Please enter EmployeeID!")
    Else
        Image1.ImageUrl = "~/HttpHandler.ashx?EmployeeID=" & EmployeeIDTextBox.Text
    End If
End Sub

这是处理程序:

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

    'context.Response.ContentType = "text/plain"
    'context.Response.Write("Hello World!")

    Dim EmployeeID As Integer
    If (Not (context.Request.QueryString("EmployeeID")) Is Nothing) Then
        EmployeeID = Convert.ToInt32(context.Request.QueryString("EmployeeID"))

    Else
        Throw New ArgumentException("No parameter specified")
    End If
    Dim imageData() As Byte = {}
    ' get the image data from the database using the employeeId Querystring
    context.Response.ContentType = "image/jpeg"
    ' You can retrieve this also from the database
    context.Response.BinaryWrite(imageData)

End Sub

这是我想要显示图像的图像控件(在Employee.aspx上)

<asp:Image ID="Image1" runat="server" imageUrl="HttpHandler.ashx?EmployeeID=7" />
得到了很多帮助......似乎还不够......

2 个答案:

答案 0 :(得分:0)

这些可能会有所帮助。场景类似,可以根据您的情况实现:

Display image from a datatable in asp:image in code-behind

Showing an image in listview from datatable

答案 1 :(得分:0)

这是在c#中,但会帮助某人。

 public void ProcessRequest(HttpContext context)
        {
            string querystring = context.Request.QueryString.ToString();

            try
            {
                if (querystring != null && querystring.Trim().Length == 0)
                {
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Error");
                    context.Response.Write("No");
                    return;
                }
                string DocCode = querystring;

                DataTable dt = GetDocumentInfo(DocCode); //Get Image From Database. I encapsulated it in other method.Implement it,

                if (dt.Rows.Count == 0)
                {    
                    context.Response.ContentType = "text/plain";
                    context.Response.Write("Error");
                    context.Response.Write("No");
                    return;
                }
                context.Response.Clear();
                context.Response.ClearContent();
                context.Response.ClearHeaders();
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetExpires(DateTime.MinValue);


                context.Response.ContentType = MimeMapping.GetMimeMapping(System.IO.Path.GetFileName(dt.Rows[0]["DocumentFileName"].ToString()));

                context.Response.AddHeader("content-disposition", "inline; filename=" + System.IO.Path.GetFileName(dt.Rows[0]["DocumentFileName"].ToString()));
                context.Response.Buffer = true;
                context.Response.Charset = "";


                context.Response.BinaryWrite((Byte[])dt.Rows[0]["DocumentFile"]);

                context.Response.Flush();
            }
            catch (Exception ex)
            {

                context.Response.ContentType = "text/plain";
                context.Response.Write("Error");
                context.Response.Write("No");
                return;
            }
            context.Response.End();
        }