?
Solved

Populate list of thumbnail images from database.

Posted on 2014-03-04
10
Medium Priority
?
348 Views
Last Modified: 2014-03-07
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.
0
Comment
Question by:TimHudspith
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
10 Comments
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39902673
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
 
LVL 53

Accepted Solution

by:
Scott Fell,  EE MVE earned 1000 total points
ID: 39902674
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
 

Author Comment

by:TimHudspith
ID: 39909868
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:TimHudspith
ID: 39909876
Correction, that should have been:

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

Open in new window

"
0
 

Author Comment

by:TimHudspith
ID: 39911536
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
 
LVL 59

Assisted Solution

by:Julian Hansen
Julian Hansen earned 1000 total points
ID: 39911673
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
 

Author Comment

by:TimHudspith
ID: 39911854
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
 
LVL 59

Expert Comment

by:Julian Hansen
ID: 39911898
You are welcome - thanks for the points.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question