We're exporting some data to CSV files and when using CFFILE (write/append) with CHARSET set to "utf-8", the file seems to be properly encoded, but there's not BOM (byte order mark) at the beginning of the file.
As far as I know, this is standard practice as the BOM is optional. Some applications (like Notepad) open the file just fine, and render the UTF-8 accented characters, angled quotations, etc. just fine.
Excel, however, has issues when opening the file if the BOM is not present. If I manually add the BOM to the beginning of the file using another application, Excel opens it fine (and so does Notepad).
Is there a simple / recommended way to add the BOM to the file using CFFILE or other built-in functionality?
The BOM character entry in UTF-8 is U+FEFF (65279) or EF, BB, BF in raw hex. So currently, I'm starting our file output with CHR(65279), and this does seem to work.
You should try something like:
<cfset OutputContent=chr(65279) & "abbcccdddd some arbitrary text content"> <cfset absolute_path_to_output_file="C:\Users\BKBK\Desktop\output.txt"> <cffile action = "write" file = "#absolute_path_to_output_file#" output = "#OutputContent#" charset="UTF-8">
From my previous post:
So currently, I'm starting our file output with CHR(65279), and this does seem to work.
I was just wondering if this is the "correct" way to do it, or if there was some other recommended way.
Oh, I see. I wasn't so sure about what you meant, so focused on the ColdFusion question, "Is there a simple / recommended way to add the BOM to the file using CFFILE or other built-in functionality?". So, we both have the same idea. And that is the "correct" way as far as I know.