HTTP处理程序效率低下图像阵列 [英] HTTP Handler Inefficient for an array of images

查看:154
本文介绍了HTTP处理程序效率低下图像阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回存储为 VARBINARY(MAX)图像从数据库的HTTP处理程序。在 IMAGEURL 属性转发设置为:

I have an HTTP Handler that returns images stored as VARBINARY(MAX) from a database. The ImageURL property in a Repeater is set as:

ImageUrl='<%# "~/ImageHandler.ashx?PictureId=" + Container.DataItem.ToString() %>' 

它的工作原理。然而,与此code,真正的问题是,在页面的的Page_Load 方法返回PictureId的从数据库中,那么'ImageHandler的列表。 ASHX品牌的独立的,个人通话应用于数据库中的每个图像。这是太多次往返到数据库。

It works. However, the real problem with this code is that the Page_Load method of the page returns a list of "PictureId"'s from the database, then the `ImageHandler.ashx' makes separate, individual calls to the database for each image. This is just too many round trips to the DB.

我正在寻找一种方式来获得所有图像在一杆,并在转发显示它们。将图像转换为Base64似乎是一个可行的选择,直到我意识到,没有可用的缓存。我还能怎么做呢?我很好HTTP处理程序做的工作,但它一定是在一杆。任何想法?

I'm looking for a way to get all of the images in one shot and display them in a Repeater. Converting the images to Base64 seemed like a viable option until I realized that there is no caching available. How else could I do this? I'm fine with HTTP Handlers doing the work, but it's got to be in one shot. Any ideas?

推荐答案

好了,你正在使用的方法是打算给你同样的结果在每一个人的呼叫,你正在使用PictureId获得的每一个独特的形象数据库。

Well, the approach You're using is going to give You the same result on every individual call as you're using PictureId to get every unique image in the database.

方法1。

为了实现关注你,你所能做的就是在先打数据库,并得到所有从数据库中的图像,并保持在泛型列表。现在,从泛型列表解析图像和供电中继器。

In order to achieve the concern You have, what you can do is hit the database at first and get all the images from database and keep in generic list. Now from that generic list parse the images and supply to repeater.

办法-2。我还没有试过以下code,但我希望它可以给你一个想法如何你可以得到你想要什么。

Approach-2. I haven't tried following code but I hope it may give You an idea how You could get what You want.

有关直放站控制dispalying图片:

For dispalying image in repeater control:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ConnectionStrings:ConnectionString %>" SelectCommand="SELECT Image FROM Images">
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" OnItemDataBound="Repeater1_ItemDataBound">
   <itemtemplate>
      <asp:HiddenField Value='<%# Eval("id") %>' ID="HiddenField1" runat="server" />
      <asp:Image ID="Image1" runat="server" />
   </itemtemplate>
</asp:Repeater>

和在aspx.cs

protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
   HiddenField hf =  e.Item.FindControl("HiddenField1") as HiddenField;
   if (hf != null)
   {
       string val = hf.Value;
       Image img = e.Item.FindControl("Image1") as Image;
       img.ImageUrl =  string.Format("data:image/jpg;base64,{0}",val);
   }
}

请注意:您不能使用HTTP基础上PictureId处理程序来获取所有图像的一击。

Note: You can't use HTTP Handler to get all the images in one hit based on a PictureId.

编辑 - >

除了你的缓存关注的事实是,如果没有具有图像(浏览器的HTTP资源)物理present您的服务器缓存无法通过Web浏览器提供服务。你可以做的是
1)使用客户端脚本通过建立一个API来调用服务器上的图像
2)或使用Base64转换去,因为没有其他选择,虽然会增加带宽利用率

Addition to Your caching concern the truth is, that without having the image (for browser an HTTP Resource) physically present on Your server caching can't be served by web browser. What You can do is 1) use client side script to call the images from server by building an API 2) or go with Base64 conversion as there's no other option although it will increase bandwidth utilization

您已经基本上组合在一个两个不同的问题。由于克服HTTP处理多个请求一种情况,并提供高速缓存是另一回事。

You've basically combined two different questions in one. Because overcoming HTTP Handler for multiple requests is one situation and providing cache is another.

有关网页浏览器的缓存功能你必须有自己的Web服务器上的HTTP资源(图像文件)present就像我说的,但如果您担心使用性能,然后交替到高速缓存,可以COM preSS你的形象某些COM pression机制,这将节省你的页面的某些带宽和加载时间。这里有一个很好的<一个href=\"https://trikks.word$p$pss.com/2011/11/03/com$p$pss-a-file-using-gzip-and-convert-it-to-base64-and-back-using-c/\"相对=nofollow>阅读。注释如果这仍是没有帮助或标记这是答案。

For web browser caching feature You should have http resource (image file) present on Your web server as I said but if You are concerned with performance then alternate to caching, you could compress Your image using some compression mechanism, it will save some bandwidth and load time of Your page. Here's a good read. Comment if this is still not helping or mark this as answer.

这篇关于HTTP处理程序效率低下图像阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆