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 »
10,640 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 »
10,640 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.


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
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »