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.