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
You could do the following immediately after the cffile upload tag:
<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
...
Copy link to clipboard
Copied
You could do the following immediately after the cffile upload tag:
<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>
Copy link to clipboard
Copied
Thanks, good approach. I was hoping it would as simple as changing the encoding, but this will do the job.