I used .ashx handler for getting images from database.I want to retrieve a lot of images (>1000) in this way:
<img src='GetImage.ashx?id= <%# Eval("id") %>'/>
(why I do this you can understand if read my previous question: bind database image to ItemTemplate in .ascx ).I am afraid that multipiles database querys (first query to get all id's,all others for getting image one by one) will take a lot of time,is it? What are possible solutions?
First of all the browsers did not ask the images all together, but few at a time.
Second, the handler is not use session, so its not lock the one the other and so a parallel process can be done for the image call.
The extra that I suggest to add it a cache for the browser, so when its load an image to not ask it again.
An example:
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(120));
context.Response.Cache.SetMaxAge(new TimeSpan(0, 120, 0));
but you can add more aggressive cache.
One similar question: call aspx page to return an image randomly slow
Caching is always a good idea for static'ish content that is asked for frequently.
Also if the images are relatively small you could use data uri's http://css-tricks.com/data-uris/
Say you had an Images table with ID INT, Name VARCHAR(64), MimeType VARCHAR(64), and Data VARBINARY(MAX)
SELECT Name, 'data:' + MimeType + ';base64,' + cast(N'' as xml).value('xs:base64Binary(xs:hexBinary(sql:column("Data")))', 'VARCHAR(MAX)') AS DataURI
<img src='<%# Eval("DataURI") %>' alt='<%# Eval("Name") %> />
In this way you can have 1 database query that will return everything.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With