We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

SQL images to the Web with C++ CGI

Medium Priority
218 Views
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.
Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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?

Commented:
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

Commented:
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 :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.