Skip to main content
tommuck1
Inspiring
June 14, 2022
Answered

How to change charset on a file upload

  • June 14, 2022
  • 1 reply
  • 297 views

We have been using cffile upload for many years without issue, but we have a problem with text files that get uploaded sometimes with a charset of "utf-8 with BOM". The BOM characters at the beginning of the file are interfering with the processes we do on the file. I don't see a way to change the encoding during the upload process, and I think one option might be to use cffile to read it back, change the charset, then write it back out to disk. I wonder if there is an easier approach...

 

Tom

    This topic has been closed for replies.
    Correct answer BKBK

    You could do the following immediately after the cffile upload tag:

    1. read the uploaded file;
    2.  check for the presence of a byte order mark. If there is one, remove it, then write the BOM-free content to disk.
      Something like:
    <cfset pathOfUploadedFile = cffile.serverdirectory & "\" & cffile.serverFile >
    
    <!--- Read the file --->
    <cffile action="read" file="#pathOfUploadedFile#" variable="contentOfuploadedFile">
    
    <!--- Check for Byte Order Mark, that is, chr(65279). --->
    <!--- If there is one, remove it by replacing with "".--->
    <cffile action="read" file="#pathOfUploadedFile#" variable="contentOfuploadedFile">
    <cfif findNoCase(chr(65279),contentOfuploadedFile) gt 0>
        <cfset BOMFreeFileContent=replace(contentOfuploadedFile,chr(65279),"","all")> 
        <!--- Write the BOM-free content as new file in same directory.--->
        <cfset BOMFreeFilePath = cffile.serverdirectory & "\" & "BOMFree_" & cffile.serverFile >
        <cffile action="write" file="#BOMFreeFilePath#" output="#BOMFreeFileContent#" charset="utf-8"> 
    </cfif>
    
    
    

     

    1 reply

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    June 15, 2022

    You could do the following immediately after the cffile upload tag:

    1. read the uploaded file;
    2.  check for the presence of a byte order mark. If there is one, remove it, then write the BOM-free content to disk.
      Something like:
    <cfset pathOfUploadedFile = cffile.serverdirectory & "\" & cffile.serverFile >
    
    <!--- Read the file --->
    <cffile action="read" file="#pathOfUploadedFile#" variable="contentOfuploadedFile">
    
    <!--- Check for Byte Order Mark, that is, chr(65279). --->
    <!--- If there is one, remove it by replacing with "".--->
    <cffile action="read" file="#pathOfUploadedFile#" variable="contentOfuploadedFile">
    <cfif findNoCase(chr(65279),contentOfuploadedFile) gt 0>
        <cfset BOMFreeFileContent=replace(contentOfuploadedFile,chr(65279),"","all")> 
        <!--- Write the BOM-free content as new file in same directory.--->
        <cfset BOMFreeFilePath = cffile.serverdirectory & "\" & "BOMFree_" & cffile.serverFile >
        <cffile action="write" file="#BOMFreeFilePath#" output="#BOMFreeFileContent#" charset="utf-8"> 
    </cfif>
    
    
    

     

    tommuck1
    tommuck1Author
    Inspiring
    June 15, 2022

    Thanks, good approach. I was hoping it would as simple as changing the encoding, but this will do the job.