Script function for file upload in ColdFusion 9 - coldfusion

Is there a a cfscript equivalent for cffile action="upload" in ColdFusion 9? Looking through the docs, there doesn't seem to be.
[Update] This was added in the 9.0.1 update
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSd160b5fdf5100e8f36f73035129d9e70a92-8000.html

You can easily abstract it with a user defined function.
<cffunction name="fileuploader">
<cfargument name="formfield" required="yes" hint="form field that contains the uploaded file">
<cfargument name="dest" required="yes" hint="folder to save file. relative to web root">
<cfargument name="conflict" required="no" type="string" default="MakeUnique">
<cfargument name="mimeTypesList" required="no" type="string" hint="mime types allowed to be uploaded" default="image/jpg,image/jpeg,image/gif,image/png,application/pdf,application/excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-powerpoint,application/vnd.openxmlformats-officedocument.presentationml.presentation,application/vnd.ms-excel,image/pjpeg">
<cffile action="upload" fileField="#arguments.formField#" destination="#arguments.dest#" accept="#arguments.mimeTypesList#" nameConflict="#arguments.conflict#">
<cfreturn cffile>
</cffunction>
And then use it in cfscript:
<cfscript>
// NOTE: because of how cffile works, put the form field with the file in quotes.
result = fileUploader("FORM.myfield", myDestPath);
WriteOutput(result.fileWasSaved);
</cfscript>
Note: I would be very careful how you rename this function in case Adobe does include this functionality down the road.

Not sure when this was added but CF does support file uploads in CFSCRIPT. I have been using FileUpload() for a little while. I have checked it is not a function in my MVC framework and def seems to be something unique to CF 9.01.
However, Builder 2 does not seem to like it nor can I find reference on CF 9 Docs but it does works and it is part the lasted Adobe ColdFusion 9.01, Ralio I have not checked tho
examples used:
fileUpload(getTempDirectory(),"ImageFile","","makeUnique");

Nope, but it has been requested.

Related

Scope not working outside of the CFC folder for Session scope

I recently installed ColdFusion 2018 at work and have been frustrated by the inability to get scope working correctly. As usual I put all my .cfcs into the /CFC folder, and none of them will execute without a blank application.cfm file in that folder. I have tried extending application, including application, proxy extending application, moving the CFCs to the root folder only gets me syntax error on JSON. I have read every article that I can find for the past two weeks and I am still unable to understand why scope will not work. It seems I can set session variables within the /CFC folder, but they are not available outside the folder? I have not worked with CF for a few years, but consider myself versed, and for the life of me cannot get this working. its probable that I am missing the forest due to trees, but if anyone would be willing to assist, I would be grateful.
instantiated object;
application.SessionMgr = CreateObject(this.obj,'CFC.SessionMgr').init('session');
proxy call;
cfajaxproxy cfc="CFC/SessionMgr" jsclassname="SessionMgr";
return is correct;
var s = new SessionMgr();
var setReport = s.setValue('ReportID', document.getElementById('cboReportKey').value);
alert(setReport);
however even manually setting session.ReportID = 7 will not persist outside the folder.
here is the SessionMgr.init
this is the init;
<cffunction name="init" access="public" returntype="SessionMgr" output="no" hint="I instantiate and return this object.">
<cfargument name="scope" type="string" required="yes">
<cfargument name="requestvar" type="string" default="SessionInfo">
<cfset var scopes = "application,Client,Session">
<cfif Not ListFindNoCase(scopes, arguments.scope)>
<cfthrow message="The scope argument for SessionMgr must be a valid scope (#scopes#)." type="MethodErr">
</cfif>
<cfset variables.scope = arguments.scope>
<cfset variables.requestvar = arguments.requestvar>
<cfset updateRequestVar()>
<cfreturn this>
</cffunction>
and the setValue fn
<cffunction name="setValue" access="remote" hint="I set the value of the given user-specific variable." returntype="string">
<cfargument name="variablename" type="string" required="yes">
<cfargument name="value" type="any" required="yes">
<cfset var val = arguments.value />
<cfset SetVariable("#arguments.variablename#", val) />
<cfset r = Evaluate(arguments.variablename) />
<cfreturn r />
</cffunction>
ok, after trying everything, heres the solution. extending by proxy doesnt work for this situation, tried that. What finally worked was creating an application.cfc IN the /CFC folder and stripping out all functional components from the /root application.cfc and simply ensuring the application name was the same in the stripped down version in /CFC folder as the /root cfc name. This apparently psuedo extends all the functionality from the /root application.cfc and makes everything available to the framework in the /CFC folder. Thanks to everyone here helping to get me to think outside my wheelhouse and resolving this issue.

IsSpreadsheetFile function in ColdFusion

I searched for IsSpreadsheetFile on the internet and cannot find many results. The reason I'm interested with this function is due to an article posted by Raymond Camden: ColdFusion Sample Upload and Parse an Excel File
He uses IsSpreadsheetFile function to determine if a file is a spreadsheet or not. I tested it, it works when my file extension is .xls but it doesn't work when it is .xlsx.
I cannot find any more explanation on the IsSpreadsheetFile function in ColdFusion documentation other than from Raymond's blog. How can I validate a spreadsheet other than using the IsSpreadsheetFile function?
Update:
Here are portions of my code that leads to calling IsSpreadsheetFile function:
<CFIF MyFileExt NEQ "xls" AND MyFileExt NEQ "xlsx">
<CFLOCATION url="../index.cfm?p=home&err=Please upload excel only.">
<CFELSE>
<CFSET ExcelObj = cfcomponents.Val_EXCEL(session.groupname,load_year)>
<CFSET ValidationResult = ExcelObj.IsExcelFile('GL',InstDirPath,FileName)>
....... codes continue on here after calling IsExcelFile method...
</CFIF>
Here is the IsExcelFile method:
<CFFUNCTION name="IsExcelFile" hint="Validate file.">
<cfargument name="AppName" type="String" required="TRUE">
<cfargument name="InstFileDir" type="String" required="TRUE">
<cfargument name="InstFileName" type="String" required="TRUE">
<cfset PathToExcelFile = "#Trim(arguments.InstFileDir)#/#Trim(arguments.InstFileName)#">
<cfdump var="#PathToExcelFile#"><br>
<cfdump var="#isSpreadsheetFile(PathToExcelFile)#"><br>
<cfoutput># isSpreadSheetObject(PathToExcelFile)# NOT an Object</cfoutput>
<cfabort>
......
</CFFUNCTION>
Note: The files definitely have .xlsx and .xls extensions. I created an .xls file by re-saving the .xlsx file to Excel 97-2003 Workbook (.xls) from Excel Workbook (.xlsx).

Any one using Mura, how can I safely add a globalFunctions.cfc

I am new to Mura and have a lot of existing code that I am trying to utilize. I have a globalFunction.cfc file that has a lot of functions that I need to have access to for the existing code. Previously I always extended my application.cfc to the global function so they where always there. With Mura I am not sure where to include it and still keep the installation "upgrade safe".
Any suggestions are appreciated.
In your [site]/includes folder is an Application.cfc. I believe that is the one you are looking to have extend your globalFunction.cfc. It is update safe.
Lance,
You can just put any functions you're wanting to use throughout your site in your eventHandler or contentRenderer files in your theme's folder. These are update safe, and depending on how you're wanting to use them, you can use one for display and the other for function.
EventHandler Ex:
<!--- PAGE - Default --->
<cffunction name="onPageDefaultBodyRender" output="true" returntype="any">
<cfargument name="$">
<cfif $.getcontentID() neq "00000000000000000000000000000000001">#$.dspInclude('/themes/MYTHEME/display_objects/bodies/dsp_body_default.cfm')#</cfif>
</cffunction>
ContentRenderer Ex:
<cffunction name="removeLinks" returntype="string" access="public">
<cfargument name="str" default="" required="true">
<cfset str=reReplace(str, "<[[:space:]]*[aA].*?>(.*?)<[[:space:]]*/[[:space:]]*a[[:space:]]*>","\1","all") />
<cfreturn trim(str) />
</cffunction>
The EventHandler here just puts out an different body if its on the home page, where the contentRenderer removes any links if i use $.removeLinks(MYURLSTRING).
HTH

Coldfusion: splitting a tag across onRequestStart() and onRequestEnd() in Application.cfc

I'm trying to see if there is a way to split a CFSAVECONTENT tag across the onRequestStart() and onRequestEnd() functions in Application.cfc to save the generated HTML of any .cfm page in the application to a variable.
Adding <cfsavecontent variable="html"> to onRequestStart() and adding </cfsavecontent> to onRequestEnd() isn't allowed since the tag must be closed in the function.
Is this even possible to do? I'm trying to avoid hard coding the CFSAVECONTENT this into every .cfm page of the site.
Thanks!
Alex,
You could do something like this in OnRequest (untested, but should work).
<cffunction name="onRequest" returnType="void">
<cfargument name="thePage" type="string" required="true">
<cfsavecontent variable="html">
<cfinclude template="#arguments.thePage#">
</cfsavecontent>
<!--- do whatever you want with the html variable here (for example, output it) --->
<cfoutput>#html#</cfoutput>
</cffunction>
I realize this has an accepted answer already, but another way to accomplish this without using cfinclude would be to use the getPageContext() object in onRequestEnd() to nab the generated content:
<cffunction name="onRequestEnd" output="yes">
<cfargument type="string" name="targetPage" required="true" />
<cfset var html = getPageContext().getOut().getString() />
<!--- Manipulate the html variable. --->
<cfoutput>#html#</cfoutput><cfabort />
</cffunction>
The <cfabort /> is important here because if you don't abort the request, the CF engine will output the generated content again and it will end up sending two copies of the output along.
I've used this method to apply site-wide changes to content on sites in a crunch where finding every instance of the original content wasn't practical or timely enough. It can also be used to send the generated content out to a translation service if needed before being returned to the end-user.

Accessing the Mura Component Bean from a Plugin

I am working on a Mura plugin that uses a mura tag to pull in a component and use it on the page. Essentially my plugin needs to call $.dspObject('component',Arguments.componentid). Since the mura scope isn't available within the plugin method then I am guessing that I need to pull in a component bean, but I have no idea how to do that.
<cffunction name="showPlayer" access="public" output="false" returntype="String">
<cfargument name="component" type="string" required="true" hint="The ID of the component that contains the playlist." />
<!--- Create the body content --->
<cfsavecontent variable="bodycode">
<cfoutput>#$.dspObject('component',Arguments.component)#</cfoutput>
</cfsavecontent>
</cffunction>
That is a very stripped down version of the method (the full eventHandler can be found on GitHub.
So how would I rewrite my code to pull the component bean out (or whatever I need to do for it)?
There are several ways possible:
<cfargument name="$">
OR
var $=application.ServiceFactory.getBean(“muraScope”);
OR
var $=getServiceFactory().getBean(“muraScope”);
OR
var $=getBean(“muraScope”);
See MuraCMS Programmers Guide and cfobject.cfc