Populate list of thumbnail images from database.

I want my webpage to work like this: on the left side is a list of photographic categories, the user clicks a category and a vertical list of thumbnail-sized image elements is populated on the right with the relevant photographs.

The images are stored in a filestream column in SQL Server. Dynamically populating a list of image elements using JQuery is no problem, it's how to grab the streamed image from the database, then convert it into the correct data type for AJAX to return to the image SRC property. I've read something about encoding the images as a base64 datatype, which then gets appended to the URL . . . can someone take me through it end-to-end?

I'm doing this using an asp.net webpage but not using any asp controls for this particular piece.
TimHudspithAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Easiest solution

Images saved as files and you send back src links.

Second easiest solution

Make your src a link to a script like so
<img src="getimage.asp?imgid=<IMAGEID>">

Open in new window


Then create a script called getimage.asp that takes an image ID as a parameter.

Get image from the database based on this parameter and stream it back to the browser.

The browser will take care of the rest.
0
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Your rendered image code will look like

<img src="data:image/jpeg;base64,/3j/4..abc" />

It is just a matter of substituting the are from my sample after the comma for your code.

<img src="data:image/jpeg;base64,<%asp_code_here%>" />
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TimHudspithAuthor Commented:
I'm getting hit and miss results with this. I encoded my image binary to a base64 string in the database, so that's all taken care of in advance. My server function just pulls out these strings using a parameterised query, wraps them in XML and sends to the browser via AJAX.

But AJAX is throwing errors for some requests and not others. I've tested the server (VB) function independently of the website and it returns correct base64 strings for all requests. Has it got something to do with the dataType and contentType parameters I'm using in AJAX?

 type: "POST",
 url: "HomePage.aspx/GetThumbnail",
 contentType: "application/json; charset=ASCII",
 data: '{keyword: "' + Keyword + '" }',
 dataType: "json"

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

TimHudspithAuthor Commented:
Correction, that should have been:

contentType: "application/json; charset=utf-8

Open in new window

"
0
TimHudspithAuthor Commented:
This is something to do with size - the length of the base64 string being returned. If the request returns a string for a single thumbnail image, then no problem, the image shows on the webpage. Also, if the request is for 2 or 3 thumbnail images that are very simple in composition (say 90% plain blue sky), then the strings are short enough not to break it, so I get those images back as well. But generally it cannot return base64 strings for 2 or more images due to some size limitation.
0
Julian HansenCommented:
Any thoughts on using the scripted solution?

<img src="getimage.asp?image_id=1" />

getimage.asp
<%
image = Request("image_id")

' Get image path from the database here
Dim strFilePath
Dim strImagePath
Dim image_type
strImagePath = "C:\Inetpub\wwwroot\test\"
strFilePath = strImagePath & "image1.jpg"

' Get image type from database or from image
image_type = "jpeg"

' Expand to set correct content type here
Response.ContentType = "image/" & image_type
Const adTypeBinary = 1

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = adTypeBinary
objStream.LoadFromFile strFilePath

Response.BinaryWrite objStream.Read

objStream.Close
Set objStream = Nothing
%>

Open in new window

page.html
<!doctype html>
<html>
<body>
<img src="getimage.asp?image_id=1" />
</body>
</html>

Open in new window

The advantage with this approach is that you can implement rich functionality in the getimage script. By using different parameters you can send back thumbnails or do a security check to see if the person requesting the image is allowed to view the image etc.
Obviously if you are storing your images in the database then this might not work for you - however, I have never really seen the logic in storing images in a database. The filesystem is far more efficient than a database.
0
TimHudspithAuthor Commented:
I've got the base64 approach working now. To lift the json limit I needed to change these settings in the web.config file:

  <system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="10000000"/>
      </webServices>
    </scripting>
  </system.web.extensions>

  <system.web>
    <httpRuntime maxRequestLength="10000" executionTimeout="10800" /> 
  </system.web>

Open in new window


MaxRequestLength is in KBs, and executionTimeout is in seconds. I set mine to the values above and can now retrieve 150 thumbnails instantly using the base64 approach.

Julian H - I'm not using that technique on this occasion but will need it in future so thanks.
0
Julian HansenCommented:
You are welcome - thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.