Link to home
Start Free TrialLog in
Avatar of wrussell051197
wrussell051197

asked on

SQL images to the Web with C++ CGI

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.
ASKER CERTIFIED SOLUTION
Avatar of alamo
alamo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of wrussell051197
wrussell051197

ASKER

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