check dsn exists
<!— This is to verify the dsn —>
<!— this is the dsn —>
<cfset var dsn = “mydsn”>
<cftry>
<!— this is to create the object of the service factory —>
<cfobject action=”create” type=”java” name=”servicefac” class=”coldfusion.server.ServiceFactory”>
<!— getting the data source service —>
<cfset datasourcesrvce =servicefac.getdatasourceservice()>
<!— checking whether the dsn exists —>
<cfset existdsn = datasourcesrvce.verifydatasource(dsn)>
<!— dsn exists if not going to catch —>
<cfcatch type=”any”>
<!— dsn dont exist —>
<cfset existdsn=”false”>
</cfcatch>
</cftry>
cfscript library
This is how we can use cfscripts
Setting a normal variable
<cfscript>
myvariable = “balu”;
</cfscript>
Do / While Loop
<cfscript>
myvariable = 1;
do {
myvariable = myvariable + 1;
WriteOutput(myvariable);
} while (myvariable LTE 0);
</cfscript>
While Loop
<cfscript>
myvariable = 0;
while (myvariable LT 10) {
myvariable = myvariable + 1;
WriteOutput(myvariable);
}
</cfscript>
Try / Catch Statements
<cfscript>
try {
x = 10 / 0;
}
catch(Any e) {
WriteOutput(“Error occured ” & e.message);
}
</cfscript>
Single Line Comments
<cfscript>
a = a + 10; //This is a single line comment
</cfscript>
Multi-Line Comments
<cfscript>
/* This is a multi line comment
that can written in
multiple lines
*/
</cfscript>
FOR Loop
<cfscript>
for (i=1;i LTE ArrayLen(array);i=i+1)
{
WriteOutput(array[i]);
}
</cfscript>
Switch Case
<cfscript>
switch(myvalue) {
case “1″:
WriteOutput(“1 is the value”);
break;
case “2″:
WriteOutput(“2 is the value”);
break;
default:
WriteOutput(“default value”);
}
</cfscript>
If… else….
<cfscript>
if (myvariable EQ 1) {
WriteOutput(“1 is the value”);
}
else if (myvariable EQ 2) {
WriteOutput(“2 is the value”);
}
else {
WriteOutput(“default value”);
}
</cfscript>
CFQuery in CFScript
Normally we use like
<cfquery name=”getstudents” datasource=”dsnstudents”>
select stuname,stuage,stuclass from students where stuid > 1
</cfquery>
In CFscript
<cfscript>
objFactory = CreateObject(”java”,”coldfusion.server.ServiceFactory”);
objDataService = objFactory.DataSourceService;
objDataSource = objDataService.GetDataSource(”dsnstudents”);
objConnection = objDataSource.GetConnection(Enter the username and password here);
objstatementstring = “select stuname,stuage,stuclass from students where stuid > 1?;
objStatement = objConnection.PrepareStatement(objstatementstring);
objResults = CreateObject(”java”,”coldfusion.sql.QueryTable”).Init( objStatement.ExecuteQuery() );
objConnection.Close();
</cfscript>
cfcontent issue error in safari
Normally in Coldfusion, we use ccontent in the format,
<!—this is the path of the file—>
<cfset fullpath = #ExpandPath(“myfolder”)#&”\”&”filename.xls”>
<!—adding the file name—>
<cfheader name=”content-disposition” value=”attachment;filename=”filename.xls””>
<!—adding the description—>
<cfheader name=”Content-Description” value=”This is a tab-delimited file.”>
<cfcontent type=”application/msexcel; charset=utf-8? reset=”true” file=”#fullpath#” deletefile=”true”>
By this, we will be able to download files from ie or mozilla. This wont work in Safari. In Safari, when we click the link we will be able to download the file with the name of the cfm file in which the code is written.
Solution
This is because the filename and the header is not getting assigned properly.
<!—this is the path of the file—>
<cfset fullpath = #ExpandPath(“myfolder”)#&”\”&”filename.xls”>
<!—adding the file name—>
<cfheader name=”content-disposition” value=”attachment;filename=””filename.xls”””>
<!—adding the description—>
<cfheader name=”Content-Description” value=”This is a tab-delimited file.”>
<cfcontent type=”application/msexcel; charset=utf-8? reset=”true” file=”#fullpath#” deletefile=”true”>
In here we are surrounding the filename with an extra (“”).
<cfheader name=”content-disposition” value=”attachment;filename=””filename.xls”””>
Leave a Comment