【问题标题】:How to pass image to Eval method from handler.ashx to imageurl?如何将图像从 handler.ashx 传递给 Eval 方法到 imageurl?
【发布时间】:2014-08-30 12:01:18
【问题描述】:

我想从数据库中检索图像并显示在 aspx 页面中。我使用 Linq 到 SQL。还有一个通用处理程序。

Handler2.ashx 代码:

public void ProcessRequest(HttpContext context)
{
    if (context.Request.QueryString["id"] != null)
    {
        int id;
        string sid = context.Request.QueryString["id"];
        if (int.TryParse(sid, out id))
        {
            Stream strm = getImage(id);
            byte[] buffer = new byte[4096];
            int i = strm.Read(buffer, 0, 4096);
            while (i > 0)
            {
                context.Response.OutputStream.Write(buffer, 0, 4096);
                i = strm.Read(buffer, 0, 4096);
            }
        }
        else
        {
            //
        }

    }
}


public Stream getImage(int id)
{
    using (DummyDBEntities cntx = new DummyDBEntities())
    {
        var db = from c in cntx.Images
                    where c.imageId == id
                    select c.imageData;
        MemoryStream ms = new MemoryStream();
        byte[] byteArray = new byte[db.ToArray().Length];
        ms.Write(byteArray, 0, db.ToArray().Length);

        return new MemoryStream(byteArray);

    }
}

当我单击时,Default.aspx 页面中的按钮控件重定向到 handler1.ashx。从数据库中获取图像的 id 并在 Default.aspx asp:image 控件中显示它

protected void btnGetID_Click(object sender, EventArgs e)
{
  int id=Convert.ToInt32(txtGetID.Text);
  Response.Redirect("Handler2.ashx?id="+id);
  Image1.ImageUrl = '<%# "~/Handler2.ashx?id=" + Eval("imageData"); %>';
}

我应该如何编写 Eval 方法和查询字符串来将图像传递给 imageurl?

Image1.ImageUrl = '<%# "~/Handler2.ashx?id=" + Eval("imageData"); %>';

请帮忙,谢谢。

【问题讨论】:

  • 如果你在重定向,你还能看到更新后的 Image1 吗?
  • Response.Redirect 会将浏览器重定向到一个完全不同的页面。所以 Image1 将不可见。
  • 首先它重定向到 Handler2.ashx 页面,将一些流转换为字节,然后从这里继续代码: Image1.ImageUrl = '';
  • 你应该阅读Response.Redirect msdn.microsoft.com/en-us/library/ms524309(v=vs.90).aspx 特别是这部分code execution in the current page is terminated when the Redirect method is processed, so subsequent code in the page will also be ignored.
  • Response.Redirect 重定向浏览器,你不会在default.aspx 了,它只会在屏幕上显示图像而不是别的。

标签: asp.net image eval generic-handler imageurl


【解决方案1】:

我希望我能正确理解您的问题。 您想在 Image1 中显示来自处理程序的图像。为此,您可以执行以下操作

Image1.ImageUrl = ResolveUrl("~/Handler2.ashx?id=" + id);

【讨论】:

  • 其实这类代码只重定向到Handler1.ashx页面。我想做的是;获取特定图像的 id 后,在 控件内的 Default.aspx 中显示图像..
  • 我认为我应该通过查询字符串将图像列数据(id 和 imageData)传递给 Default.aspx,并使用一些 Eval() 方法在 asp:image 控件中显示图像。但我不知道该怎么做..
  • 对不起,为什么要重定向到handler1.ashx或handler2.ashx?如果您只是将 url 分配给 image1,那么它会负责从 url 中检索图像,因为您的 ashx 会将二进制数据写入响应。
  • 这是我第一次使用通用处理程序。我一直在寻找从数据库中检索图像并在 asp 控件中显示。我遵循了本教程:dotnetcurry.com/showarticle.aspx?ID=129。这就是我这样做的原因。那么,你的意思是我可以不使用处理程序来做到这一点吗?
  • 处理程序很好。请按照教程进行操作。您已经编写了接受 id 作为查询字符串参数的处理程序,从数据库中检索图像并将图像写入响应。要从此处理程序在图像控件中显示图像,您只需使用查询字符串参数将图像控件的 ImageUrl 属性分配给此处理程序
猜你喜欢
  • 1970-01-01
  • 2022-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多