Solved

Populate list of thumbnail images from database.

Posted on 2014-03-04
10
339 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
  • 4
  • 3
10 Comments
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
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 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 total points
Comment Utility
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
Comment Utility
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
 

Author Comment

by:TimHudspith
Comment Utility
Correction, that should have been:

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

Open in new window

"
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:TimHudspith
Comment Utility
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 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Julian Hansen
Comment Utility
You are welcome - thanks for the points.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 …
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now