Renaming Files As They Are Uploaded (how CFFILE actually works)

8 March 2007 11:12 (EST)
When receiving a file uploaded from a client, you can actually save it and rename it all in one step rather than two steps as the documentation implies.

The documentation states that the "destination" attribute of the CFFILE tag specifies the "pathname of the directory in which to upload the file." This is misleading for two reasons:

The file has actually already been uploaded by the time your CFFILE tag is encountered. The file has automatically been saved in ColdFusion's temporary directory. Using the CFFILE tag with the action attribute set to "upload" is really just moving the file from one place to another. If no CFFILE tag is encountered, the file is simply deleted.
You can actually specify a file name in addition to a directory in the destination attribute. Rather than this...

<cffile action="upload" destination="/path/to/some/directory" ... />
<cffile action="rename" ... />

... you can just do this...

<cffile action="upload" destination="/path/to/some/directory/#createUUID()#.gif" ... />

This moves the uploaded file from ColdFuson's temporary directory into the specified directory, and renames it using a unique ID generated by ColdFusion, all in one step.

Write Out Binary Data

8 March 2007 11:11 (EST)
In case CFContent is restricted on server:

<cffile action="readbinary" file="/home/cantrell/Pictures/Corrs2.jpg" variable="pic"/>
<cfscript>
context = getPageContext();
context.setFlushOutput(false);
response = context.getResponse().getResponse();
out = response.getOutputStream();
response.setContentType("image/jpeg");
response.setContentLength(arrayLen(pic));
out.write(pic);
out.flush();
out.close();
</cfscript>

CFXM 7 Hosting

8 March 2007 11:10 (EST)
HostingFuse Optima — AU$99/year (see details)
CrystalTech — US $16.95/month

Looping dates and times

8 March 2007 11:09 (EST)
<cfset startDate = Now()>
<cfset backDate = DateAdd("yyyy",-6,startDate)>
How Far Back do you want to go:
<select name="goBack">
<cfloop condition="startDate gt backDate">
<cfoutput><option>#DateFormat(startDate,"mm/dd/yyyy")#</option></cfoutput>
<cfset startDate = DateAdd("yyyy",-1,startDate)>
</cfloop>
</select>
<hr>
10 Day Loop<br>
<cfloop from="#Now()#" to="#DateAdd("d",9,Now())#" index="i">
<cfoutput>#DateFormat(i,"mm/dd/yyyy")#<br></cfoutput>
</cfloop>
<hr>
Days left to the end of the month:<br>
<cfloop from="#now()#"
to="#DateAdd("d",DaysInMonth(now()) - DatePart("d",now()),now())#"
index="i" step="#CreateTimeSpan(0,23,59,59)#">

<cfoutput>#DateFormat(i,"mm/dd/yyyy")#<br></cfoutput>
</cfloop>
<hr>
Time Loop<br>
<cfset startTime = CreateTime(0,0,0)>
<cfset endTime = CreateTime(23,59,59)>
<cfloop from="#startTime#" to="#endTime#" index="i"
step="#CreateTimeSpan(0,0,30,0)#">

<cfoutput>#TimeFormat(i, "hh:mm tt")#<br /></cfoutput>
</cfloop>

Display datasources

8 March 2007 11:08 (EST)
<cfif left(server.ColdFusion.ProductVersion,1) le "5">
<cfregistry action="getall" name="DataSources" type="any" sort="entry"
branch="HKEY_LOCAL_MACHINE\SOFTWARE\allaire\coldfusion\currentversion\data-sources">

<cfoutput query="datasources">#datasources.entry#<br></cfoutput>
<cfelseif left(server.ColdFusion.ProductVersion,1) ge "6">
<cffile action="read" file="#server.coldfusion.rootdir#\lib\neo-query.xml" variable="thisxml">
<cfwddx action="wddx2cfml" input="#variables.thisxml#" output="aDsnInfo">
<cfset ldsn = "">
<cfloop collection="#aDsnInfo[3]#" item="thisdsn">
<cfset stInfo = aDsnInfo[3]>
<cfset stInfo = stInfo[thisdsn]>
<cfif stInfo.driver is "MSSQLServer" or stInfo.driver is "MSAccess" or stInfo.driver is "MSAccessJet">
<cfset ldsn = listappend(ldsn,variables.thisdsn)>
</cfif>
</cfloop>
<cfset ldsn = listsort(ldsn,"TextNoCase")>
<cfloop list="#ldsn#" index="thisdsn">
<cfoutput>#variables.thisdsn#<br></cfoutput>
</cfloop>
</cfif>

Flushing cached queries

8 March 2007 11:07 (EST)
<cflock name="serviceFactory" type="exclusive" timeout="10">
<cfscript>
factory = CreateObject("java", "coldfusion.server.ServiceFactory");
ds_service = factory.datasourceservice;
ds_service.purgeQueryCache();
</cfscript>
</cflock>

List MX Datasources

8 March 2007 02:01 (EST)
<cfobject component="cfide.administrator.components.administrator" name="admin">
<cfset admin.login("MY_SECRET_ADMIN_PSWD")>
<cfobject component="cfide.administrator.components.datasource" name="ds">
<cfdump var="#ds.getdatasource()#">
<cfset admin.loginout()>

Cool tip

8 March 2007 02:00 (EST)
Using a server on which you have access to CFIDE and the Aministrator password, add /CFIDE/componentutils/componentdoc.cfm to the URL, enter the CF Administrator password and prepare to be utterly amazed.

Faster SQL Deletes

8 March 2007 02:00 (EST)
Want to delete all data in a table? You can use DELETE FROM TABLE, but that is slow because changes are logged. A faster solution is to issue a TRUNCATE TABLE command which is faster because no logging occurs (and if really intent to delete all the data in a table, logging is probably not that important).

How To Access Badly Named Form Fields

8 March 2007 01:58 (EST)
Sometimes you need to process the results of a form which was made up of fields named with invalid characters (one was "first name"). Trying to access the field as #FORM.first name# won't work and will throw an error. So how to access these fields?

The answer is to remember that FORM is not just a prefix, it is also a structure. Structure members can be accessed as #structure.member# and as #structure["member"]#. And that latter format will allow access to badly named form fields (as the name is enclosed in quotes). So, the solution is to use #FORM["first name"]#.
Pages previous   next
3 4 5 6 7