Skip to main content
Inspiring
January 7, 2021
Answered

Adding UTF-8 BOM (Byte Order Mark) To File Output

  • January 7, 2021
  • 1 reply
  • 2611 views

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.

 

Thanks

    Correct answer bw_bloodletter

    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.

    1 reply

    BKBK
    Community Expert
    Community Expert
    January 9, 2021

    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">
    bw_bloodletterAuthorCorrect answer
    Inspiring
    January 12, 2021

    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.

    BKBK
    Community Expert
    Community Expert
    January 12, 2021

    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.