Skip to main content
Ben Nadel at LaunchDarkly Lunch & Learn 2018 (New York, NY) with: Lena Krug and Adam Zimman and Christopher Cosentino and Greg Ratner
Ben Nadel at LaunchDarkly Lunch & Learn 2018 (New York, NY) with: Lena Krug ( @LenaLovesStuff ) Adam Zimman ( @azimman ) Christopher Cosentino ( @chriscosentino ) Greg Ratner ( @GregRatner )

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

Published in Comments (9)

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.

<!--- Set the header info to force that file attachment. --->
	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.
	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.

Want to use code from this post? Check out the license.

Reader Comments



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).


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



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/">
<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?


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.


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.


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?

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel