When images are associated with database records, those images are usually stored on disk. The images are related to their respective records by storing their names in a table or perhaps by using the record's identifier as part of the file name. The advantages of storing images as files are performance (no database access needed to retrieve the file, and likely nothing but straight HTTP access), and flexibility.
But sometimes you may indeed need to store images within database tables (perhaps as blobs). Storing the images is easy enough, serving them dynamically is a little more complex. You can't simply use the column name in the IMG SRC attribute as you would static image files, although ColdFusion would indeed embed the image data within the tag, the browser would not display the image as intended.
When browsers render pages containing images they do so by making multiple requests, first for the page itself, and then for each image, making an HTTP request to obtain the URL named in the IMG SRC tag. But that URL need not be an actual file, it can also be a dynamic URL that returns an image.
Therefore, you can create a .cfm file that does nothing more than serve images. The code would work like this:
<!--- Determine image to retrieve --->
<cfset ...>
<!--- Get image --->
<cfquery datasource="mydsn" name="image">
SELECT imageData
FROM table
WHERE ...
</cfquery>
<!--- Set MIME type to GIF or JPEG or ... --->
<cfcontent variable="image.imageData" ... >
This file would be saved on the server, and the URL to it (with the image identifier passed as a URL parameter) can now be used as an IMG SRC like this:
<img src="/getImage.cfm?imageid=123">
And that'll do it.
<img src="..."> is not working for me.
I have my main file 'testdhs.cfm':
<cfquery name="rs1" datasource="dhsdb">
SELECT IMAGE
FROM dbo.CASEIMG
WHERE IMAGE_ID = #URL.imageid#</cfquery>
<cfcontent type="image/tiff" variable="#rs1.IMAGE#">
Then, in the page that will display the tiff,
<img src="/cms_temp/testdhs.cfm?imageid=0" />
This page displays the broken image icon "red X". However, when I check the properties of the broken image, it displays the proper URL, which when copied and pasted into the browser's address bar, opens a dialog box asking if I want to open or save the .tif . If I open it, a viewer pops up and displays the image I wanted.
So, why doesn't the img src work? Do I need to include something else on that page?
I have decided to use OBJECT tags to utilize QuickTime. The TIFF now displays in my browser.
If anyone else has a cleaner, better way to display TIFF's inside a browser, post away. thanks