0

page.aspx.cs 代码:

HttpContext.Current.Session["id1"] = id1.ToString();
Server.Transfer("Handler.ashx"); //Getting error
Image1.ImageUrl = "~/Handler.ashx?ID=" + id1.ToString();

HttpContext.Current.Session["id2"] = id2.ToString();
Server.Transfer("Handler.ashx"); //Getting error
Image2.ImageUrl = "~/Handler.ashx?ID=" + id2.ToString();

Handler.ashx 代码:

public void ProcessRequest(HttpContext context)
{
    int id = Convert.ToInt32(context.Session["id1"]);
    byte[] IMG = class.ReadImg(id);
    context.Response.ContentType = "image/jpg";
    context.Response.BinaryWrite(IMG);
}
public void ProcessRequest2(HttpContext context2)
{
    int id = Convert.ToInt32(context2.Session["id2"]);
    byte[] IMG2 = class.ReadImg(id);
    context2.Response.ContentType = "image/jpg";
    context2.Response.BinaryWrite(IMG2);
}

page.aspx 代码:

<asp:Image ID="Image1" runat="server" />//How to define thats gonna show image of id1
<asp:Image ID="Image2" runat="server" />//How to define thats gonna show image of id2

问题:

  1. 我无法将数据从 page.aspx 传输到 Handler.ashx
  2. 我不知道如何制作Image元素来根据 id 来识别它应该显示的图像(也许是一些request.queryString我不明白的魔法)

帮助!!谢谢 :]

4

1 回答 1

1

Server.Transfer() 执行它所说的 - 在服务器端传输请求,而不向客户端返回 HTTP 301 重定向。

只需读取 ashx 文件中的查询字符串即可解决您的问题:

string id = context.Request.Querystring["ID"];
context.Response.ContentType="image/jpeg";
......

然后在您的 aspx 页面中:

Image1.ImageUrl = "/Handler.ashx?ID="+id.ToString();
于 2012-07-02T17:12:51.800 回答