Coldfusion Output to .txt file

A couple of methods I use to output a file from the browser to .txt.

Write your query as normal. Then use one of the two output methods below.

CF8 – Output as comma delimited with quotes. Note, without quotes the line does not break properly.
<cfcontent type="text/plain">
<cfheader name="content-disposition" value="attachment;filename=yourfilename.txt">
"Header-numericfield","Header-alphafieldname"
<cfoutput query="queryname">#numericfield#,"#alphafieldname#"
</cfoutput>

CF7 – Output as tilde delimted with line wraps defined.
<cfcontent type="text/plain">
<cfheader name="content-disposition" value="attachment;filename=yourfilename.txt">
Header-numericfield~Header-alphafieldname~/n
<cfoutput query="queryname">#numericfield#~#alphafieldname#~/n
</cfoutput>

It is important to note that wherever you have line breaks and spaces in your code, you will have linebreaks and spaces in your txt file. This can be a problem if users are trying to upload your data into a database.

Cup size   
Select size then click on coffee cup.
This entry was posted in Code, Coldfusion, Snippets. Bookmark the permalink.