• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

cfwrite HTNL to txt file with weird characters

New Here ,
Jan 25, 2022 Jan 25, 2022

Copy link to clipboard

Copied

HI

I have CF 9, try to query from DB with utf-8 chars.  it show Ok on web page.

Then we tried to write to txt file so we can download and open the Excel.

the txt file shows &#... chars

Please help.  I tried to google but can not find and help.

I really appreaciate.

I will buy you a Starbuck coffee.

 

<!--- Lets make the file, and put the first row of Column headings in --->
<cffile action="WRITE" file="#f_dir##f_name#" output="CamperType, FullName, LDName, MCNName, CamperID, Photo" addnewline="Yes" charset="windows-1252">
<cfloop query="camperList">
<cffile action="APPEND" file="#f_dir##f_name#" output="#camperTypeName#, #camperFullName#, #Ldname#, #mcnName#, #camperID#, #photoIDFile#" addnewline="Yes" charset="utf-8">
<!--- End the loop here --->
</cfloop>
<br>
<cfoutput>
<a href="reportDownload.cfm?FileName=#absolutefilepath#">Here is the file</a>
</cfoutput>

Views

88

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 25, 2022 Jan 25, 2022

Copy link to clipboard

Copied

charset="windows-1252" charset="utf-8"

 

Any reason why you use "windows-1252"? I expected you to use UTF-8 charset everywhere. 

 

In fact, I would suggest that you:

  1.  Open /bin/jvm.config in an editor and add the flag -Dfile.encoding=UTF-8 to java.args.
  2.  Then restart ColdFusion

 

As a last resort (if all of the above fails), you could use the class org.apache.commons.lang.StringEscapeUtils to convert HTML entities. For example:

<!--- Lets make the file, and put the first row of Column headings in --->
<cffile action="WRITE" file="#f_dir##f_name#" output="CamperType, FullName, LDName, MCNName, CamperID, Photo" addnewline="Yes" charset="utf-8">

<cfloop query="camperList">
<cfset outputRowData = "#camperTypeName#, #camperFullName#, #Ldname#, #mcnName#, #camperID#, #photoIDFile#">
	<cffile action="APPEND" file="#f_dir##f_name#" output="#convertHTMLEntity(outputRowData)#" addnewline="Yes" charset="utf-8">
	<!--- End the loop here --->
</cfloop>

<br>

<cfoutput>
<a href="reportDownload.cfm?FileName=#absolutefilepath#">Here is the file</a>
</cfoutput>

<cffunction name="convertHTMLEntity" returntype="String">
	<cfargument name="inputString" required="yes">

	<cfset var conversionObject = createObject("java", "org.apache.commons.lang.StringEscapeUtils") />
	
	<cfreturn conversionObject.unescapeHTML(arguments.inputString)>

</cffunction>

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 30, 2022 Jan 30, 2022

Copy link to clipboard

Copied

LATEST

Did that help?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation