【问题标题】:database Image not getting displayed asp.net数据库图像未显示 asp.net
【发布时间】:2013-03-05 05:51:55
【问题描述】:

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

Employee.aspx 上的显示按钮

 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" />

得到了很多帮助...似乎还不够...

【问题讨论】:

  • 当你把HttpHandler.ashx?EmployeeID=7的完整url直接放到浏览器中,图片能打开吗?如果没有,您应该先修复该部分。
  • 它不会...如果我知道如何解决它...
  • context.Response.ContentType = "image/jpeg" 将是必需的。确保类型正确
  • 类型正确...

标签: asp.net


【解决方案1】:

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

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

Showing an image in listview from datatable

【讨论】:

  • 你检查过 Telerik 二进制图像控制吗?
  • 看到这个,因为它描述了你正在处理的问题:aspalliance.com/…
  • 所以现在我必须使用 ajax 工具包??
  • 不,它独立于 Ajax。
  • 我不太确定......我什至不知道如何使用它......我想我会让事情变得更加复杂......
【解决方案2】:

这是在 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();
        }

【讨论】:

    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 2016-03-14
    • 2013-09-21
    • 1970-01-01
    • 2012-07-18
    • 2021-03-02
    相关资源
    最近更新 更多