views:

308

answers:

2

Is there a a cfscript equivalent for cffile action="upload" in ColdFusion 9? Looking through the docs, there doesn't seem to be.

A: 

Nope, but it has been requested.

CF Jedi Master
It's odd upload wasn't included, considering the number of file functions that were added.
Ryan McIlmoyl
+5  A: 

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.

Dan Sorensen
That's exactly what I have done in the past, I was just hoping I could avoid it in 9. Maybe in 10.
Ryan McIlmoyl