HTTPHandler 将二进制数据写入 Response ,加载图像
iv'e 得到了一个绑定到实体列表的转发器
BL_Engine engine = (BL_Engine)Application["engine"];
List<AppProduct> products = engine.Get_Products();
Repeater1.DataSource = products;
Repeater1.DataBind();
iv'e 还得到了一个用户控件,我用它来表示这些产品实体,我通过重写用户控件中的 Databind() 来实现这一点:
public override void DataBind()
{
AppProduct product = (Page.GetDataItem() as AppProduct);
lbl_title.Text = product.Title;
lbl_short.Text = product.Short;
lbl_date.Text = product.Date.ToShortDateString();
Session["current_img"] = product.Image1;
base.DataBind();
}
在我的 HttpHanlder 对象中,保存在 . ashx 文件我将图像写入响应 响应仅发生一次,因此只有最后一张图片被写入(全部)用户控件。
public void ProcessRequest(HttpContext context)
{
byte [] image = (byte[])context.Session["current_img"];
context.Response.ContentType = "image/bmp";
context.Response.OutputStream.Write(image, 0, image.Length);
}
知道如何为每个单独的控件编写二进制数据,
提前致谢 埃兰。
iv'e got a repeater bonded to a list of entities
BL_Engine engine = (BL_Engine)Application["engine"];
List<AppProduct> products = engine.Get_Products();
Repeater1.DataSource = products;
Repeater1.DataBind();
iv'e also got a user control i use to represent these product entities , i do this by overriding the Databind() in the user control :
public override void DataBind()
{
AppProduct product = (Page.GetDataItem() as AppProduct);
lbl_title.Text = product.Title;
lbl_short.Text = product.Short;
lbl_date.Text = product.Date.ToShortDateString();
Session["current_img"] = product.Image1;
base.DataBind();
}
in my HttpHanlder object kept in a .ashx file i write the image to the response
the response happens only once so only the last picture is written to (ALL) the user controls.
public void ProcessRequest(HttpContext context)
{
byte [] image = (byte[])context.Session["current_img"];
context.Response.ContentType = "image/bmp";
context.Response.OutputStream.Write(image, 0, image.Length);
}
any idea how i could write the binary data for each individual control
thanks in advance
eran.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让我建议一种不同的方法。
在您的控件中声明一个常规 html 图像元素,并按如下方式设置“runat=server”属性:
然后更改您的
DataBind()
方法来执行此操作:并摆脱 HTTPHandler。你不需要它。
Let me suggest a different approach.
Declare a regular html image element in your control and set the "runat=server" property as so:
Then change your
DataBind()
method to do this instead:And get rid of the HTTPHandler. You don't need it for this.
更改您的处理程序,使其将当前产品的 id 作为查询字符串参数。在处理程序中,根据参数加载正确的图像数据并将其写入。
Change your handler so that it takes the id of the current product as a query string parameter. In the handler load the correct image data based on the parameter and write that instead.
总而言之,对于这种情况,我认为最好的做法是@icarus的答案,
不是无视@kmcc049的答案,我只是没有深入研究它,因为它对于我的应用程序来说似乎是一个更复杂的架构。
在这种情况下,删除 HTTPHANDLER 。
我保存了图像的类型以及发布文件中的数据。
然后我提取并将其添加到我的 DataBind 覆盖中的用户控件(在我的用户控件中):
well to conclude , for this case i think the best practice would be @icarus's answer ,
not the disregard @kmcc049's answer i just hadn't dug into it since it seemed like a more complicated architecture for my app.
in this case DROP THE HTTPHANDLER .
i saved the image's type , and data from the post file.
then i extracted and added it to the user control in my DataBind override(in my user control) :