27 November 2008

Downloading files from the database with CF

If you use your database as a file store and coldfusion as your webserver, you'll doubtless one day want to download those files from a cfm page.

here are two useful snippets that had me stuck for a while:

To download the image:

< cfheader name="Content-Disposition" value="attachment; filename=" >
< cfcontent type="#fileType#/#fileSubtype#" variable="#fileData#" >


ALWAYS CHECK:
this is important, you need to enable blob downloads in cfadmin.

  1. Open up your datasource in cfadmin,

  2. click advanced,

  3. check the checkbox marked: "Enable binary large object retrieval (BLOB)."

04 November 2008

coldFusion and SQL Express

Hey Boys and girls. I recently tried to install ColdFusion 8 and SQL express 2005.

Quite easy but a couple of points to remember for next time:

1) TCP/IP needs to be enabled in sql connection manager (weird that its disabled)
2) TCP/IP needs to be given a default port (cf uses 1433)

After you've done this restart sql (or the whole computer) and you should be ready to go.

Thanks

24 October 2008

hash function in sql

Just stumbled across this cool sql 2005 feature:

SELECT HashBytes('MD5','password')

Why did no one tell me about this before? This could have saved me HOURS of short pointless cf script writing.

29 September 2008

SQL Update

Try as i might, i NEVER remember how to do this:

UPDATE table1
SET snakesOnAPlane = t2.data
FROM table1 t1
JOIN table2 t2 ON t2.row1 = t1.row1
WHERE t1.row3 = 'helloWorld'

22 September 2008

SQL Transaction

bit like in cfmx here's a useful bit of t-sql / sql for rolling back on error.

BEGIN TRAN
insert into ()--....sql goes here

IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
END
COMMIT TRAN

It's not perfect but it seems to work...sometimes....if the wind is blowing north, and it's a wednesday.

19 August 2008

Cold Fusion reload web service

ColdFusion does a very enthusiastic job of caching web services, which if you're still developing or making a few changes can be frustrating. I've found this code to be VERY useful to reload a web service cached by cfadmin.


<cfobject type="JAVA"
action="Create"
name="factory"
class="coldfusion.server.ServiceFactory">



<cfset RpcService = factory.XmlRpcService />



<cfset RpcService.refreshWebService("http://myURL/webservices/mycfc.cfc?WSDL")>

javascript web service

OK so AJAX / JS...whateves right?

This little bit of code allows you to call a web service from JavaScript. I have found many articles and ways of doing this on the interweb and non of them seem to work. This one works with CF web services.

The interesting thing i found is you don't need to pass parameters using the XMLHttpRequest method, to call a specific function just append &method= to the end of the wsdl string.


function ajaxFunction()

{

var xmlHttp;

try

{

// Firefox, Opera 8.0+, Safari

xmlHttp=new XMLHttpRequest();

}

catch (e)

{

// Internet Explorer

try

{

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");

}

catch (e)

{

try

{

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");

}

catch (e)

{

alert("Your browser does not support AJAX!");

return false;

}

}

}

xmlHttp.onreadystatechange=function()

{

if(xmlHttp.readyState==4)

{

document.getElementById("myDivResponse").innerHTML = xmlHttp.responseText;

}

}

xmlHttp.open("GET","http://myurl/webserrvices/mycfc.cfc?WSDL&method=getSomething",true);

xmlHttp.send(null);

}