December 6, 2004

Using ColdFusion to work with SQL binary data

<CFSET FileName = "HotDogVendor.jpg">
<CFFILE ACTION="readbinary" FILE="D:\WebFiles\MyWebsite\Images\Postcards\#FileName#" VARIABLE="NEWDATA">

<cfset NewObject = toBinary(NEWDATA)>

<cfquery name="insertImage" datasource="MyDatasource" username="myusername" password="mypassword">
update images
set content = <CFQUERYPARAM VALUE="#NewObject#" CFSQLType="CF_SQL_BLOB">
where NAME = '#FileName#'
</cfquery>

...And now, how to output an image, which is stored in a SQL Server table as an image data type? Here is code which I found on another forum that appears to work (but still not the perfect solution):

<cfquery name="GetImage" datasource="MyDatasource" username="myusername" password="mypassword">
select content
from Images
where NAME = '#FileName#'
</cfquery>

<cfcontent type="image/gif; charset=8859_1">

<CFSCRIPT>
writeOutput(toString(GetImage.Content));
</cfscript>

No comments: