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,243 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,243 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 23, 2013 at 3:55 AM
Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()
very interesting and helpful too. ... read »
May 22, 2013 at 5:35 PM
Script Tags, jQuery, And Html(), Text() And Contents()
This is still an issue 2 years later. jQuery is supposed to remediate these cross browser issues, no? I have been unable to find any statement from the jQuery team calling this behavior "by de ... read »
May 22, 2013 at 12:44 PM
Ask Ben: Query Loop Inside CFScript Tags
In cf10, if you call a function that has: local.result = {}; local.result.msg = ""; local.svc = new query(); local.svc.setSQL("SELECT * FROM..."); local.obj = local.svc.exe ... read »
May 22, 2013 at 12:29 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben: What version of Java are you using? Also, did you test users.id to see what Java reports as the data type? I wonder if it's not a Java primitive data type, but getting returned as something ... read »
May 22, 2013 at 11:47 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dana, Awesome - so it looks like this bug was fixed in ColdFusion 10. Thanks so much for double-checking that. ... read »
May 22, 2013 at 11:37 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
When I c&p and run on cf10, I get: Selected User IDs: 1,4 User 1 selected: YES - YES User 2 selected: NO - NO User 3 selected: NO - NO User 4 selected: YES - YES User 5 selected: NO - ... read »
May 22, 2013 at 11:27 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Tom, Good thought, but no dice. Both of these still exhibit the same behavior: users.id[ users.currentRow ] users[ "id" ][ users.currentRow ] It's just something whacky happening with ... read »
May 22, 2013 at 11:07 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
Could your problem be that "users.id" is actually an ARRAY, not a single value? Perhaps try it again with "users.id[1]" (I only have CF8 here at work). ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools