Remote Synthesis
Search my blog:
Viewing By Entry / Main
Jul 15, 2009

Multi-file Uploads with ColdFusion 9 in 5 Lines

ColdFusion 9 includes a new Flash-based page element for handling multiple file uploads. Many people have spent quite a bit of time solving this problem, but using ColdFusion 9, you could add this functionality to your site in just about 5 lines. It includes the functionality you'd expect from a multi-file upload element including things like the ability to specify a maximum number of files or a maximum file size. It's also fairly customizable using styles. So let's look at a basic example.

At its core, the new <cffileupload> tag only needs to be supplied a URL for the processing page. It also does not need to be placed inside a <cfform>. Here's my example page which will create a form element in the image on the right:

<html>
<head>
   <title>CFFileUpload Example</title>
</head>
<body>
<cffileupload url="process.cfm" />
</body>
</html>

In order to handle the processing, a new action has been added to the <cffile> tag called "uploadall" which only requires the destination be supplied. Technically speaking, that's all you need on your processing page to make the form work. However, I ran into technical issues with this that the ColdFusion team helped me out with. The upload has an onComplete attribute that calls a JavaScript and passes it a json string. Even though I had not set that on my cffileupload, it seems that was causing my upload to bomb out after the first item. The lines following the cffile tag resolved that issue. This is why the title says 5 lines instead of 2 - but 5 lines for this level of functionality is still pretty amazing.

<cffile action="uploadall" destination="#expandPath('uploads')#" nameconflict="overwrite" />

<cfset str.STATUS = 200>
<cfset str.MESSAGE = "passed">
<cfoutput>#serializeJSON(str)#</cfoutput>

Obviously, in many cases you'll want to mess with more of the customization than in this very simple example. If you want to take a closer look at the customization options, the documentation for the tag can be found here.

Comments
Ben Nadel
Not having to be *inside* cfform is the awesome! Thanks for the tip. Now, I will probably use this :)


Dave Ferguson
Very nice. Now to find a use for it.


Sam Farmer
Another cool addition...


Dan Vega
Of course if your not on CF9 yet you could always check out http://cfmu.riaforge.org :)


amclean
Are the last 3 lines just a temporary workaround until they get out of beta, or is this the expected final coding result?
Coldfusion tends to have that stuff already taken care of so it would be counterintuitive to require it now I think.


Brian Rinaldi
@amclean - I would suspect its just a workaround that will be fixed.


Paul
Could this be used for secure file uploads?

My security system involves checking session variables for each page to ensure the user has privileges to see the page. On loading the response page specified by the cffileupload url attribute, I can't send in the session variables, so the user cannot access the response page and the file is not uploaded.


Tony Bentley
Looks like a winner. I know in the past, when dealing with flash, I needed to pass session.URLToken to force ColdFusion to use the proper session on the post page.


Shane Heasley
Sweet. But does it use the old CFFILE code where the entire file is loaded into memory or the code used in the CF8 FileOpen, FileRead, etc. functions?


Brian Rinaldi
@Shane - functions like FileOpen, FileRead, etc are for reading and writing files on the server not for uploading files from the client. It uses cffile as in the example above.


Shane Heasley
Brian:

Very much appreciate the answer but I use FileOpen etc. to upload files from the client - the huge advantage being that the entire file does not have to be loaded into memory. This allows for uploading of very large files and avoids the server issues that can arise if multiple large files are uploading simultaneously.


Shane Heasley
Brian.

Mea culpa - I was suffering from a serious case of public stupidity. Please ignore my last two entries. I seemed to have forgotton some basic htmil theory. UG.


Write your comment



(it will not be displayed)