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

How to change charset on a file upload

Explorer ,
Jun 14, 2022 Jun 14, 2022

Copy link to clipboard

Copied

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

Views

166

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

correct answers 1 Correct answer

Community Expert , Jun 15, 2022 Jun 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, remo
...

Votes

Translate

Translate
Community Expert ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

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>


 

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
Explorer ,
Jun 15, 2022 Jun 15, 2022

Copy link to clipboard

Copied

LATEST

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

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