SQL images to the Web with C++ CGI

Posted on 1998-07-01
Last Modified: 2013-11-18
Ok, this is a pretty involved question, and can go in about 3 different groups, but here goes...

What I'm trying to do, is get an image from a MS-SQL 6.5 server, and display it on a web page. The web page is currently generated by a CGI program written in C++, and already interacts with the database, just not image data. Am I going to have to create a file everytime I want to display an image stored in the database, or is there some shortcut to dump this binary data straight to the web browser? All of the images will be stored in the database in the same format (.JPG) as thats what I seem to get the best compression with.
Question by:wrussell051197
  • 2
  • 2

Accepted Solution

alamo earned 100 total points
ID: 1167012
You can't send the image to the browser from the same instance of the cgi that generates the html page. You have to generate the html with image tags which reference the desired image(s), and then the browser will turn around and request the image from you in a separate request. This request could be almost immediate, or slightly delayed (if the user has images turned off but chooses to load your image) or much later (if the user bookmarks the image).

There are two main ways to generate the images. The first is for the html-producing cgi to generate a temporary .jpg file which the IMG tag references. The second is for the IMG tag to reference a second cgi (or the same cgi with an additional parameter that indicates the cgi should produce the image not the html). The image-producing cgi needs enough information passed as parameters to be able to retrieve the image data from the database. It can return an image rather than html by producing the correct headers (i.e. Content-type: mage/jpeg instead of Content-type: text/html).

Each way has its pluses and minuses, and it depends on your requirements. The temporary file method is slightly more efficient since once the file has been created the server handles it directly with no cgi, but there is the overhead of having to do periodic cleanup of old files and ensuring that you have the necessary disk space. Images are also not bookmark-able which might be a plus or a minus depending on your application. Generating the image via cgi is slightly slower but an easier overall solution. It isn't practical if the information needed to fetch the image can't easily be included in the IMG parameters, though. It also generates more database accesses, which on a high-traffic or connection-limited server could be a problem.

That was the overview, let me know if you have any questions or need more detail. Good luck!

Author Comment

ID: 1167013
Ok, I'd like to do it without generating a temporary file, would be much easier for what I need... let me see if I have this correct...

My first cgi program should generate something like:

<img src="\cgi-bin\getimage.exe?image=1035" Height = 100 Width = 100>

at which point the browser will then want getimage.exe which should send

Content type: image/jpeg

and then what? Just the raw binary data? Or does it need to be encoded or what?

Expert Comment

ID: 1167014
Yes, the img tag is correct, except you'd use / not \ in the URL.

Your getimage cgi first has to print the headers, which at a minimum is just
Content type: image/jpeg
followed by a blank line to signal the end of the headers and the beginning of the data (i.e. "Content type: image/jpeg\r\n\r\n"). You can include other headers, of course but the above will work by itself. It's a nice touch to include a "Content-length: xxxxx" header giving the size in  bytes of the image, so the user gets a % complete display as its loading.

After the headers, you just print the raw binary data, with no encoding at all. You may need to set STDOUT for binary access, rather than text, so that the system doesn't helpfully insert \n into your binary data wherever it sees \r :-)

Author Comment

ID: 1167015
Thanks! I'll give it a try and see how it works :)
I always have trouble with slashes, I have to go back and change about half of them after I write anything :)

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

Shoutout to Emily Plummer ( for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

821 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