Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute

Posted November 27, 2006 at 9:23 AM

Tags: ColdFusion

If you haven't used ColdFusion's CFContent tag to stream data to the browser, either as an inline object, or as an attachment, you should at least test it out. It's a really great feature of ColdFusion and really gives you some good control (with CFHeader) over how the data gets used by the end user. Anyway, some one over on CF-Talk (a while back) asked about using CFContent's VARIABLE attribute to stream text to the browser as an attachment. The Variable attribute of CFContent only takes binary objects (such as binary graphic data). You could however, store the text to a temporary file and then use CFContent's FILE attribute to stream the file. The user in question, however, did not want to store the text to a temp file but wanted to stream it using Variable.

The question then becomes, how to get the text to fit into a binary hole? The answer? Convert it to binary. I had suggested this, but had not actually tested it until now:

 Launch code in new window » Download code as text file »

  • <!---
  • Store the text you want to stream. This could be done
  • here, or it could be gotten from a different part of
  • the application.
  • --->
  • <cfsavecontent variable="strText">
  • This is some cool text.
  • </cfsavecontent>
  •  
  • <!--- Set the header info to force that file attachment. --->
  • <cfheader
  • name="Content-Disposition"
  • value="attachment; filename=streamed_text.txt"
  • />
  •  
  • <!---
  • Stream the text. To do so, we have to convert it to
  • Base64 first, then convert that to binary.
  • --->
  • <cfcontent
  • type="text/plain"
  • reset="true"
  • variable="#ToBinary( ToBase64( strText ) )#"
  • />

This works like a charm. Now, you could have just written the text to the response stream and it would have done the same thing. So why do it this way? Not exactly sure. I would think the best reason would be able to have a centralized file streaming template that always takes a binary object. In that case, you would have to convert the text to binary before hand, but the idea is the same.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

Reader Comments

Dec 22, 2006 at 2:18 PM // reply »
1 Comments

FYI this only works with ColdFusion 7, CF6 and older don't have the VARIABLE attribute.


Dec 22, 2006 at 2:22 PM // reply »
6,516 Comments

Damien,

Very true. I have run into that problem a few times. When that happens, at least with CFMX 6, you can at least stream a file, but it takes a few extra steps (ex. writing text to a temp file).


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 20, 2009 at 11:32 PM
Five Months Without Hungarian Notation And I'm Loving It
I've used headless camel case for years for not only ColdFusion variables, but also SQL tables and fields... pretty much everything involving code. I also subscribe to the "don't abbreviate and clea ... read »
Nov 20, 2009 at 11:00 PM
Five Months Without Hungarian Notation And I'm Loving It
@Marcel, Yeah, I always err on the side of longer but more readable variable names. As for the camel casing of CF methods and the headless camel casing of custom items, I get around this by always ... read »
Nov 20, 2009 at 10:56 PM
Five Months Without Hungarian Notation And I'm Loving It
I use the following and love it: my.namespace.MyComponents.functionMethodsOrUDF() CONSTANT_VALUES_OR_PROPERTIES One thing I always try is to CamelCaseBuiltInColdFusionFunctions() so others can tell ... read »
Nov 20, 2009 at 5:38 PM
Learning ColdFusion 8: CFImage Part I - Reading And Writing Images
Hi Ben, Great article. I've been looking around to see if ColdFusion image engine can programatically create the following "wrap around" effect: http://www.creativepro.com/article/photoshop-s-she ... read »
Nov 20, 2009 at 5:35 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Dave: I talked to Gert he suggested: <cfhttp method="get" url="http://{some cf website}" result="stuff" addtoken="yes" /> Note the addition of cfhttp attribute addtoken. That should persist y ... read »
Nov 20, 2009 at 5:23 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
@Todd, Ahh, gotcha, yeah that makes sense. ... read »
Nov 20, 2009 at 5:17 PM
Maintaining ColdFusion Sessions Across SMS Text Message Requests Without Cookies
Ben, sorry if I didn't make this clear. You can make it work like that if you want, just put <cfset session.foo = 1> (and <cfset application.foo = 1>) in your OnRequestStart() and it reve ... read »