Multi-file Uploads with ColdFusion 9 in 5 Lines

Posted on Jul 15, 2009

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 :)

Posted By Ben Nadel / Posted on 07/15/2009 at 10:06 AM


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

Posted By Dave Ferguson / Posted on 07/15/2009 at 10:12 AM


Sam Farmer Another cool addition...

Posted By Sam Farmer / Posted on 07/15/2009 at 11:30 AM


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

Posted By Dan Vega / Posted on 07/15/2009 at 11:52 AM


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.

Posted By amclean / Posted on 07/15/2009 at 3:47 PM


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

Posted By Brian Rinaldi / Posted on 07/15/2009 at 4:31 PM


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.

Posted By Paul / Posted on 08/05/2009 at 4:28 PM


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.

Posted By Tony Bentley / Posted on 10/27/2009 at 4:19 PM


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?

Posted By Shane Heasley / Posted on 12/07/2009 at 3:38 PM


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.

Posted By Brian Rinaldi / Posted on 12/07/2009 at 6:56 PM


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.

Posted By Shane Heasley / Posted on 12/07/2009 at 8:56 PM


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.

Posted By Shane Heasley / Posted on 12/08/2009 at 11:17 AM


Ric I used your exact example to try this out. The files however will not upload using Safari or Firefox. The uploader goes through all the motions and even says everything was a success but the files are not in the detination folder.

In IE8 the files upload no problem.

Anyone know how to fix it so this example works in safari and firefox?

Thanks

Posted By Ric / Posted on 06/10/2010 at 10:03 PM


Xvpulzxa This is your employment contract <a href=" http://www.netvibes.com/ynyofujyl#Preteens_In_Nylon ">Preteens In Nylon</a> >:-) <a href=" http://www.netvibes.com/laooduq#Preteen_Art_Sites ">Preteen Art Sites</a> :-OOO <a href=" http://www.netvibes.com/fylecuqor#Preteen_Galleries_Stockings ">Preteen Galleries Stockings</a> pjq <a href=" http://www.netvibes.com/pebejutol#Pre_Teen_Tranny ">Pre Teen Tranny</a> 35176 <a href=" http://www.netvibes.com/upugakypap#Pre_Teen_Pantiehose ">Pre Teen Pantiehose</a> unyv <a href=" http://www.netvibes.com/ikoahado#Russian_Preteenz_Cp ">Russian Preteenz Cp</a> 886256 <a href=" http://www.netvibes.com/makanuhyn#Gay_Preteens_Nude ">Gay Preteens Nude</a> 52596 <a href=" http://www.netvibes.com/atemacyra#Preteen_Nude_Slut ">Preteen Nude Slut</a> fduogn <a href=" http://www.netvibes.com/rahomynou#Preteen_Jerking_Video ">Preteen Jerking Video</a> >:DDD <a href=" http://www.netvibes.com/uticonose#Preteen_Teens_Peeing ">Preteen Teens Peeing</a> %]]]

Posted By Xvpulzxa / Posted on 01/31/2012 at 9:45 AM


Write your comment



(it will not be displayed)





About

My name is Brian Rinaldi and I am the Web Community Manager for Flash Platform at Adobe. I am a regular blogger, speaker and author. I also founded RIA Unleashed conference in Boston. The views expressed on this site are my own & not those of my employer.