Skip to main content
Participant
January 25, 2022
Question

cfwrite HTNL to txt file with weird characters

  • January 25, 2022
  • 1 reply
  • 134 views

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>

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    January 25, 2022

    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>

     

    BKBK
    Community Expert
    Community Expert
    January 30, 2022

    Did that help?