Using Coldfusion to create a CSV file

I am trying to create a CSV file and I am getting extra text in my file that I don't know where it is coming from. At the top of my file I am getting the following added:
<script type="text/javascript">/* <![CDATA[ */_cf_loadingtexthtml="<img alt=' ' src='/CFIDE/scripts/ajax/resources/cf/images/loading.gif'/>";
_cf_contextpath="";
_cf_ajaxscriptsrc="/CFIDE/scripts/ajax";
_cf_jsonprefix='//';
_cf_clientid='ED371EBC09E21BE8E48FF7867E65CFBF';/* ]]> */</script><script type="text/javascript" src="/CFIDE/scripts/ajax/messages/cfmessage.js"></script>
<script type="text/javascript" src="/CFIDE/scripts/ajax/package/cfajax.js"></script>

<script type="text/javascript">/* <![CDATA[ */
      ColdFusion.Ajax.importTag('CFAJAXPROXY');
/* ]]> */</script>

After that part my CSV data is added and that part is correct. To create my CSV file I am doing the following:
<cfheader name="Content-Disposition" value="attachment;filename=Custom_Data_#Dateformat(now(),"mm_dd_yyyy")#.csv">
			<cfcontent type="text/csv">
			<cfoutput>#savecontent#</cfoutput>	

Open in new window


What is causing the extra text to be added to my file?
WestCoast_BCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_agx_Commented:
>  that part is correct.

Not sure what you mean by that.  Output the value of #savecontent# by itself. Does it contain the extra text? If it does, that suggest the text is being added on the page generating that variable.  If not, how is this download page called?
WestCoast_BCAuthor Commented:
savecontent is simple a variable that contain the CSV data that I want to save. I have tested the contents of this variable and it is correct, ie, it does not contain the extra text that is being added to the file.
WestCoast_BCAuthor Commented:
I don't know if this is helpful but I tried creating a sample file that looks like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
	<cfset newline = Chr(13) & Chr(10)>		
	<cfset delimiter="#chr(9)#">
	<cfsavecontent variable="savecontent">
		<cfoutput>
		column1#delimiter#column2#delimiter#column3#newline#
		data1#delimiter#data2#delimiter#data3#newline#
		data4#delimiter#data5#delimiter#data6#newline#
		data7#delimiter#data8#delimiter#data9#newline#
		</cfoutput>
	</cfsavecontent>
	<cfheader name="Content-Disposition" value="attachment;filename=Custom_Data_#Dateformat(now(),"mm_dd_yyyy")#.xls">
	<cfcontent TYPE="application/msexcel">
	<cfoutput>#savecontent#</cfoutput>	

</body>
</html>

Open in new window


and the output that I get is:
		column1	column2	column3

		data1	data2	data3

		data4	data5	data6

		data7	data8	data9
	

</body>
</html>

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

WestCoast_BCAuthor Commented:
I don't understand why in my previous example the
</body>
</html>

Open in new window

is added
_agx_Commented:
It's mostly due to the <cfcontent> tag. By default it resets the buffer, causing and any text before the tag to be ignored. So the </body></html> tags at the end are treated as literal text.

That said, there shouldn't be any HTML tags in a CSV download. The only thing the page should output is the CSV string:

<!--- this will generate a warning in newer versions of Excel ---->
<cfheader name="Content-Disposition" value="attachment;filename=Custom_Data_#Dateformat(now(),"mm_dd_yyyy")#.xls">
<cfcontent TYPE="application/msexcel">
<cfoutput>#savecontent#</cfoutput>	

Open in new window

WestCoast_BCAuthor Commented:
This still doesn't explain my initial problem which is why am I getting :
<script type="text/javascript">/* <![CDATA[ */_cf_loadingtexthtml="<img alt=' ' src='/CFIDE/scripts/ajax/resources/cf/images/loading.gif'/>";
_cf_contextpath="";
_cf_ajaxscriptsrc="/CFIDE/scripts/ajax";
_cf_jsonprefix='//';
_cf_clientid='ED371EBC09E21BE8E48FF7867E65CFBF';/* ]]> */</script><script type="text/javascript" src="/CFIDE/scripts/ajax/messages/cfmessage.js"></script>
<script type="text/javascript" src="/CFIDE/scripts/ajax/package/cfajax.js"></script>

<script type="text/javascript">/* <![CDATA[ */
	ColdFusion.Ajax.importTag('CFAJAXPROXY');
/* ]]> */</script>

Open in new window

added to the beginning of my CSV file which is what is happening in my application.

The code that produces the CSV file resides in a CFFunction. What confuses me is that I know in a previous version of Coldfusion this was working. I haven't used this code in a while so I am now puzzled why it is suddenly not working since the only real change I believe is just which version of Coldfusion is being used.
WestCoast_BCAuthor Commented:
Is there a better way to create a CSV file that a user can download?

Also, I see that I should probably be using cfspreadsheet to create my excel file instead of using cfcontent. Am I correct?
_agx_Commented:
This still doesn't explain my initial problem which is why am I getting :

Yes but like I said, if it's not part of the content variable, then it must be added when the download is invoked.  That's a part of your code we can't see.  To track it down, you need to post the code of the pages involved.


Is there a better way to create a CSV file that a user can download?

There's nothing glaringly wrong with what you've posted. For the most part, it works exactly as is. So again, the problem is in some part of your code that we can't see. Based on the code added, it's most likely a page with some CF ajax components.

This sample component based on the example you posted works fine under CF10, ie http://localhost/test/YourComponent.cfc?method=getCSVContent

YourComponent.cfc
<cfcomponent>

	<cffunction name="getCSVContent" output="true" access="remote">
		<cfset Local.newline = Chr(13) & Chr(10)>		
		<cfset Local.delimiter = chr(9)>
		<cfsavecontent variable="Local.savecontent">
			<cfoutput>
			column1#delimiter#column2#delimiter#column3#newline#
			data1#delimiter#data2#delimiter#data3#newline#
			data4#delimiter#data5#delimiter#data6#newline#
			data7#delimiter#data8#delimiter#data9#newline#
			</cfoutput>
		</cfsavecontent>
		<cfheader name="Content-Disposition" value="attachment;filename=Custom_Data_#Dateformat(now(),"mm_dd_yyyy")#.xls">
		<cfcontent TYPE="application/msexcel">
		<cfoutput>#Local.savecontent#</cfoutput>	
	</cffunction>
</cfcomponent>

Open in new window

WestCoast_BCAuthor Commented:
I am wondering if there is something funny about my localhost. I just tried the same code on a test site using my ISPs server and it all seems to work fine.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
_agx_Commented:
Edit
Could be.  Did you try the code above on localhost? If you still get the same results, try it in a totally separate directory, not covered by an Application.cfc.

If it works correctly there, that suggests a problem with the application code and how the download is invoked.
If it is still broken, that points to a possible problem with the install.
Gurpreet Singh RandhawaCEOCommented:
try to check if you are using cfahax tags or cfform tags anywhere in your application, if yes, try commenting those tags and convert cfform to form tag and see again, this will make sure that cfide scripts are not included by default
_agx_Commented:
Well, not just anywhere.  As I mentioned above,  the results suggest that the pages involved in calling the download contain CF Ajax components.  Other, less likely, possibilities are server problem or code within in the Application.cfc. However, the results of the tests above should give a good indication which is most likely.
WestCoast_BCAuthor Commented:
Problem is only happening on my local machine using localhost and not on my live site.  I am not going to work on this anymore since it might be an issue with a setup on my machines
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.