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.
Want to use code from this post? Check out the license.
FYI this only works with ColdFusion 7, CF6 and older don't have the VARIABLE attribute.
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/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?
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.
Yes, exactly! Since XML is so sensitive to leading white-space, this is a perfect place to use this.
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?
Your blogs are very informative and helpful.