Unable to create a utf-8 file

Panos
Panos used Ask the Experts™
on
Hello experts.
I'm trying to create a utf-8 file using the line:
<cffile action = "write" file="#path##linkalias#.cfm" output="" mode="666" charset="utf-8">
but unfortunately the created file has ANSI charset.
(i'm checking this with notepad)
Any help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2011

Commented:
- you need to add the content type in the beginning of that page:
<cfcontent type="text/html; charset=utf-8">
<cfprocessingdirective pageEncoding="utf-8">

- there is also another question on UTF8 in EE that you might want to go thru:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_27109857.html
Brijesh ChauhanStaff IT Engineer

Commented:
How are you checking that the file created has ANSI charset ?

<cffile action = "write" file="#path##linkalias#.cfm" output="" mode="666" charset="utf-8"> - this will create a BLANK .cfm file, so how are you checking on the charset ??
 

Author

Commented:
Hi OP Zaharin.
The link you are referring to is a question i made on 21/06/11. I had to change all the created files from dreamweaver ,from ansi to utf-8 and add the <cfcontent type="text/html; charset=utf-8"> line in the application.cfm page and <cfprocessingdirective pageEncoding="utf-8"> in the top of all pages even the cfinclude pages. That is the trik and unfortunately i could not "see" the problem until the day i uploaded the site to a hoster with a none greek operating system.
In this question the problem is to create a utf-8 file using cffile.
Using the above lines will not solve the problem.

Hi brijeshchauhan
I'm checking the files with notepad.
The created pages are not blank.
Here is some more code .I hope it helps.

<cfset directive = '<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">'>

 <cfif NOT directoryexists('#path#')>
   <cfdirectory action="create" directory="#path#" mode="666">
 </cfif>
<cfif  directoryexists('#path#')>
 <cffile action = "write" file="#path##linkalias#.cfm" output="" mode="666" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#directive#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#form.details#" addnewline="yes" charset="utf-8">

As you see i'm creating the file if it is not there , add the line for the charset and after that the content of a form textarea.

By the way this action page  has utf-8 charset too, and on the top the line
<cfset directive = '<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">'>

Staff IT Engineer
Commented:
hmmm.. let us try to add the COMPLETE charset and other tags..

can you try below code..

<cfset directive = '<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">'>
<cfset headTags = '
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>

<body>'
>

<cfset endTags = '</body>
</html>'
>

<cfset endProcessingDirective = '</CFPROCESSINGDIRECTIVE>'>

 <cfif NOT directoryexists('#path#')>
   <cfdirectory action="create" directory="#path#" mode="666">
 </cfif>
<cfif  directoryexists('#path#')>
 <cffile action = "write" file="#path##linkalias#.cfm" output="" mode="666" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#directive#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#metaType#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#form.details#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#endTags#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#endProcessingDirective#" addnewline="yes" charset="utf-8">

Open in new window


Author

Commented:
Hi again.
The new generated file is now utf-8.
But i don't need the doctype and meta tags because i'm creating cfinclude files. Any idea?

Author

Commented:
The line that is making the difference is the meta tag.
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

If i keep  this line the file is in utf-8. deleting that line i get an ansi file
Brijesh ChauhanStaff IT Engineer

Commented:
yes, you need to keep the meta tag to give the content-type, you can remove ALL other not required elements and just keep the meta tag..

<cfset directive = '<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">'>
<cfset headTags = '
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>'
>

<cfset endTags = '</body>
</html>'
>

<cfset endProcessingDirective = '</CFPROCESSINGDIRECTIVE>'>

 <cfif NOT directoryexists('#path#')>
   <cfdirectory action="create" directory="#path#" mode="666">
 </cfif>
<cfif  directoryexists('#path#')>
 <cffile action = "write" file="#path##linkalias#.cfm" output="" mode="666" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#directive#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#metaType#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#form.details#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#endTags#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#endProcessingDirective#" addnewline="yes" charset="utf-8">

Open in new window

Author

Commented:
brijeshchauhan
Is this normal having the meta tag line in all cfinclude files?
Having this in the head is ok but in the body??

I have not seen this anywhere.
Brijesh ChauhanStaff IT Engineer

Commented:
No.. You need to have it in HEAD..

so probably you will need a CFIF condition, that if it is the FIRST time you are creating the file, then include the META tag, otherwise do not include it..



Author

Commented:
I'm trying to understand .
if i it is the FIRST TIME i create the file a get a meta in the top that i have to delete it some how.
As i said it will be a cfinclude file for the body of a page.

If i use f.e action = "write" twice (the second time without the tags) this will recreate the file and the problem remains. Any idea?

Brijesh ChauhanStaff IT Engineer

Commented:
@panos

You can have a .cfm file with complete HTML TAGS..

say file ->

a.cfm -> this can have the complete HTML tags, including it's own META tags, so you can have your a.cfm as

<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<!--- FORM CONTENTS HERE --->
</body>
</html>
</CFPROCESSINGDIRECTIVE>

2. You can include this a.cfm, using <cfinclude .. in ANY of your files, it would work, please remember the CLOSING TAGS as well.. the complete example is my comment ID 36333543

In general, all included files will get the META TAGS for the parent file...

Author

Commented:
Here is the problem.
I use the cficlude to "include" formfields.
I have created files with formelements in divs.
Depending on what a client is looking for, i create the form i want with the form elements  i need , getting the information dynamic.
f.e for category 1 i will get :(field1.cfm,field4.cfm....)
<cfloop query="getfields">
    <cfinclude template="../attributes/#fields#">
   </cfloop>    
I don't want to use the cfswitch and cfcase for that.

I hope you understand me.
Brijesh ChauhanStaff IT Engineer

Commented:
hmmm..

Any reason you want to create .cfm files ?

You can have the formfields in <cfsavecontent> tag and then display them, instead of CFINCLUDE ..

below is say what you can rplace field1.cfm wih .. a variable field1

<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">
<cfsavecontent variable="field1">
      <cfcontent type="text/html; charset=utf-8">
      <cfoutput> #form.details# </cfoutput>
</cfsavecontent>
</CFPROCESSINGDIRECTIVE>

then in your loop you can say

<cfloop query="getfields">
      <cfoutput> #fields#</cfoutput>
</cfloop>  

Author

Commented:
The idea was to get the content from a page instead of the db.

Well using <cfsavecontent tag is a very good idea. I will try this. By the way can i create ONE cfm page with all the cfsavecontents and  include this with cfinclude ?(to have a separate for page )
Brijesh ChauhanStaff IT Engineer

Commented:
yes, you can ..

Author

Commented:
Ok.
Thank you very much for your help

Author

Commented:
thank you again.
regards
panos

Author

Commented:
brijeshchauhan please a little help because i can't get it work.
i have now a file like
<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">
<cfsavecontent variable="price">
      <div class="detaildiv <cfif backset MOD multiple EQ 0>f1f1f1</cfif>">
       <div class="DtdL inline">#request.preis#:</div>
       <div class="DtdR inline">#LSCurrencyFormat(qryitem.Price)#
       </div><cfset backset = backset + 1 >
      <div class="clearboth"></div>
</cfsavecontent>
</CFPROCESSINGDIRECTIVE>

My query is like
<cfquery name="getfields" datasource="#request.dsn#">
 SELECT  ff.f_fields_file FROM category_fields cf
 LEFT JOIN form_fields ff ON cf.c_fields_field_id = ff.f_fields_id
 WHERE c_fields_mobile_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.category#">
</cfquery>

i did change the data in the db from price.cfm  to price

The output in your example is returning the text 'price' because that's in the db.
how can i output the <cfsavecontent variable="price">
Brijesh ChauhanStaff IT Engineer

Commented:
Try this out...

CFPROCESSINGDIRECTIVE pageencoding="UTF-8">
<cfsavecontent variable="request.price">
      <div class="detaildiv <cfif backset MOD multiple EQ 0>f1f1f1</cfif>">
       <div class="DtdL inline">#request.preis#:</div>
       <div class="DtdR inline">#LSCurrencyFormat(qryitem.Price)#
       </div><cfset backset = backset + 1 >
      <div class="clearboth"></div>
</cfsavecontent>
</CFPROCESSINGDIRECTIVE>

Open in new window


<cfquery name="getfields" datasource="#request.dsn#">
 SELECT  ff.f_fields_file FROM category_fields cf 
 LEFT JOIN form_fields ff ON cf.c_fields_field_id = ff.f_fields_id
 WHERE c_fields_mobile_id = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.category#">
</cfquery>

Open in new window


<cfoutput> #request.getfields['f_fields_file'][1]# </cfoutput>

Open in new window

Author

Commented:
I got this error:
Element GETFIELDS is undefined in a Java object of type class [Ljava.lang.String;.
Brijesh ChauhanStaff IT Engineer

Commented:
The last code ..

<cfset formName = getfields.f_fields_file />
<cfoutput> #request.formName# </cfoutput>

Open in new window

Author

Commented:
error:Element FORMNAME is undefined in REQUEST.

i did use another field because the request.price exists
so i have :
<cfsavecontent variable="request.default_top">

the default_top value exists .I made a test using <cfoutput>#formname#</cfoutput> and i got 'default_top'
but for : <cfoutput> #request.formName# </cfoutput> i get the error above

Author

Commented:
The following code is working:
<cfset fields = '#default_top#'>
   <cfloop list="#fields#" index="i">
   <cfoutput> #i# </cfoutput>
</cfloop>
but i don't know if this better than that what i had in the beginning
Brijesh ChauhanStaff IT Engineer

Commented:
hmmm.. did it solve your CHARSET issue ?

Author

Commented:
I have two choices.
1. Use the cfinclude files like in the beginning but changing manually the charset.
2. Use the cfsavecontent where i can change once the charset and then use append function for each field. But for the output i have to output a list and after that loop through it....

Because i want to output a detailpage for transporters where every typ needs  different fields i have a little problem with the cfsavecontent way because it is executing unneccesary code. (for fields that i'm not going to output)

Well what is your opinion. Have in mind that after i will get a working solution i'm not going to make changes to the pages but only to 'what fields' i 'm going to use.
Brijesh ChauhanStaff IT Engineer

Commented:
>> Use the cfinclude files like in the beginning but changing
manually

Open in new window

the charset. -- anything manual is NOT GOOD.. !!!..

cfsavcontent is a good option.. just need to figure out on how to use it properly, I think we are missing something...

Author

Commented:
The list way is one option.
I could even check if a cfsafecontent variable is in the list -> execute the cfsafecontent..So i will not execute unneccesary code .

Well i have nothing else in mind for now.

Author

Commented:
Hi brijeshchauhan
Finally i got it work without list.
Take a look:
1.) <cfsavecontent variable="request.default_top">.....
2.) <cfloop  query="getfields">
  <cfoutput>#evaluate('request.#f_fields_file#')#</cfoutput>
  </cfloop>

You think it is better than cfinclude ?

Now for the charset i have an idea-trick
Take a look at the code below. I place the meta tag in a <cfsetting enablecfoutputonly="yes">.
The page has the meta tag code but in the html output it is not there and the generated page is utf-8.
That's All!!
<cfset directive = '<CFPROCESSINGDIRECTIVE pageencoding="UTF-8">'>
<cfset headTags = '<cfsetting enablecfoutputonly="yes"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><cfsetting enablecfoutputonly="no">'>
<cfset endTags = ''>
<cfset path = '#request.catalogpath#search#request.bslash#attributes#request.bslash#'>
<cfset linkalias = 'test'>
<cfset form.details = 'blablablablablablablabla'>
<cfset endProcessingDirective = '</CFPROCESSINGDIRECTIVE>'>

 <cfif NOT directoryexists('#path#')>
   <cfdirectory action="create" directory="#path#" mode="666">
 </cfif>
<cfif  directoryexists('#path#')>
 <cffile action = "write" file="#path##linkalias#.cfm" output="" mode="666" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#directive#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#headTags#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#form.details#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#endTags#" addnewline="yes" charset="utf-8">
 <cffile action = "append" file="#path##linkalias#.cfm" output="#endProcessingDirective#" addnewline="yes" charset="utf-8">
</cfif>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial