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

Posted November 27, 2006 at 9:23 AM by Ben Nadel

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:

  • <!---
  • 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.




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 »
11,246 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).


Nov 22, 2009 at 1:45 AM // reply »
5 Comments

The reason you would want to do this is to stream. Ack json/xml files to ria clients

I used thus technique before because putting json in response stream causes debugging info to come thru
As well as the little used cfhtmlhead text

A


Nov 17, 2010 at 9:52 AM // reply »
3 Comments

Hi,
I need to do something like that but with an excel file. The xls format is correctly opened with the file attributte:

  • <cfheader name="Content-Disposition" value="attachment;filename=file.xls">
  • <cfcontent type="application/ms-excel">

But xlsx files are not recognized. I believe it is because the mimetype, but I've tried distinct ones and doesn't work:

  • <cfcontent type="application/vnd.ms-excel.sheet.macroEnabled.12">
  • <cfcontent type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" reset="true">

The excel file (xlsx) is generated with your POIUtility.cfc (great work!) so... I have a HSSFWorkbook variable contaning the excel information... Should i convert it to binary data and use the variable attribute on cfcontent? How could i convert it?


Feb 23, 2011 at 12:24 AM // reply »
1 Comments

One would want to do the above in scenarios, where trying to return XML output, but due to the <cfoutput> tag, it generates spaces which make the XML document as invalid thought it's a valid document. So converting a byte array would only return actual XML document rather spaces in CFM file.


Feb 23, 2011 at 10:48 AM // reply »
11,246 Comments

@Pbreddy,

Yes, exactly! Since XML is so sensitive to leading white-space, this is a perfect place to use this.


Feb 25, 2011 at 2:26 PM // reply »
1 Comments

I'm using "cfcontent" to define XML as the MIMETYPE for my REST web service built in Cold Fusion. Code is: <cfcontent type="text/xml" reset="YES"> . Seems as if it works great on my Windows CF8 developer's version and my CF8 development server, however, it doesn't work on my CF7 server! No matter what I do that always returns text/html type to the browser. Seems like some older versions of cfcontent were a little flakey.


May 2, 2012 at 10:54 AM // reply »
1 Comments

They know how to apply this same but in versions prior to 7 in coldfusion?.
I am creating a excel file and the idea is to remove the warning message that appears, indicating warning the form, with the example above works but not in coldfusion 5. Is there any alternative to the variable attribute cfcontent?


Jan 8, 2013 at 2:30 AM // reply »
1 Comments

Ben,

Your blogs are very informative and helpful.

Thanks!


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 25, 2013 at 10:01 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Avi, Really glad to help! @Jaredwilli, I'm finding a this image hits home with a lot of people :) Hopefully we can all work through the rough patches together! @Prateek, AngularJS has error ... read »
May 25, 2013 at 9:53 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Mrsean2k, I'm glad I could help! I haven't been able to keep up with the ui-router stuff. I keep saying that I'll carve out time, but I just haven't gotten to it :( ... read »
May 25, 2013 at 9:49 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, Thanks for the book recommendations. I am looking them up right now. I can see that Object Thinking is available for the Kindle App - sweet! Also, I just recently heard Martin Fowler on the ... read »
May 25, 2013 at 9:41 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
@Chris, I'm super excited to hear that my posts are helpful. I am also loving AngularJS; but, it definitely has some caveats and some odd behaviors and some things that just don't seem to "wor ... read »
May 25, 2013 at 9:36 PM
Ask Ben: Manually Enforcing Basic HTTP Authorization In ColdFusion
@Adam, @Jason, After reading these comments, I double-checked my latest implementation and I am happy to report that I am using listFirst() and listRest(). ... read »
May 25, 2013 at 9:31 PM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Daxesh, I am not sure I understand the question about the current node. If you already have a reference to the current node, why would you need to query for it? As for parent node, I believe that ... read »
May 25, 2013 at 10:08 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
@Ben, my question is that i want the current node with its tag and its parent node. i just want only that data. So, give me the solution for that. and remember solution is working on " xpath 1.0 ... read »
May 25, 2013 at 10:01 AM
Using "//" And ".//" Expressions In XPath XML Search Directives In ColdFusion
hey ben, i want get my current node tag and also want the root node tag withing. So, how can i fix it.. ! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools