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
Solved

Populate list of thumbnail images from database.

Posted on 2014-03-04
10
343 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 55

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 52

Accepted Solution

by:
Scott Fell,  EE MVE earned 250 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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 55

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 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 55

Expert Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
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 embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

856 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