Avatar of Panos
Panos
Flag for Germany asked on

Unable to create a utf-8 file

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?
ColdFusion Language

Avatar of undefined
Last Comment
Panos

8/22/2022 - Mon
OP_Zaharin

- 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:
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/Cold_Fusion_Markup_Language/Q_27109857.html
Brijesh Chauhan

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 ??
 
Panos

ASKER
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">'>

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Brijesh Chauhan

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Panos

ASKER
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?
Panos

ASKER
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 Chauhan

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Panos

ASKER
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 Chauhan

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..



Panos

ASKER
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?

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Brijesh Chauhan

@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...
Panos

ASKER
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 Chauhan

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>  
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Panos

ASKER
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 Chauhan

yes, you can ..
Panos

ASKER
Ok.
Thank you very much for your help
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Panos

ASKER
thank you again.
regards
panos
Panos

ASKER
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 Chauhan

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Panos

ASKER
I got this error:
Element GETFIELDS is undefined in a Java object of type class [Ljava.lang.String;.
Brijesh Chauhan

The last code ..

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

Open in new window

Panos

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Panos

ASKER
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 Chauhan

hmmm.. did it solve your CHARSET issue ?
Panos

ASKER
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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Brijesh Chauhan

>> 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...
Panos

ASKER
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.
Panos

ASKER
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

Your help has saved me hundreds of hours of internet surfing.
fblack61