How To Upload Multiple Files With ColdFusion

Sometimes, you may need to have a customer, client or co-worker upload a single file or muliple files to the server for processing from a single online form. You could accomplish this by just letting the user upload multiple times with the same form, but we can make it a more simple stream line user interface, by allowing the user to select one or more files and upload using one submit button, or one instance of an online form.


The code below allows the user to select up to 5 files to upload to the server.

<cfform action="?uploadfiles=Yes&requesttimeout=600"
<table border=0 cellpadding=0 cellspacing=2>
<td valign=top align=right class="standard-content">FILE 1:</td>
<td valign=top><cfINPUT NAME="File1" TYPE="file" style="FONT-SIZE: 10px" required="yes" message="Please provide FILE 1"></td></tr>
<td valign=top align=right class="standard-content">FILE 2:</td>
<td valign=top><cfINPUT NAME="File2" TYPE="file" style="FONT-SIZE: 10px" ></td></tr>
<td valign=top align=right class="standard-content">FILE 3:</td>
<td valign=top><cfINPUT NAME="File3" TYPE="file" style="FONT-SIZE: 10px" ></td></tr>
<td valign=top align=right class="standard-content">FILE 4:</td>
<td valign=top><cfINPUT NAME="File4" TYPE="file" style="FONT-SIZE: 10px" ></td></tr>
<td valign=top align=right class="standard-content">FILE 5:</td>
<td valign=top><cfINPUT NAME="File5" TYPE="file" style="FONT-SIZE: 10px" ></td></tr>
<td colspan=2 valign=top class="standard-content" align="right"><input type="submit" value="Upload Files" style="FONT-SIZE: 10px"></td></tr>


The following code, assigns a variable for the path to the location of where the files will be uploaded.
Loops through 5 times, to check for files being uploaded. The parameter of 5 can be changed, just be aware of the timeout issue of too many files being uploaded.

<cfif IsDefined('uploadfiles')>

<cfparam name="xDir" default="d:\InetPub\Domains\whatsite\www\upload\">
<cfloop index="i" from="1" to="5" step="1">

<cfset filename = "form.file" & #i#>

<cfif evaluate(variables.filename) neq "">
<CFFILE ACTION="upload" FILEFIELD="#variables.filename#" DESTINATION="#xDir#"




More by this Author

  • Tips For How to Stain Concrete Floors

    You don't always have to tile, carpet, or hardwood your floors. The new thing is to just stain the concrete that's already there. It's a lot of work, but if done properly, your home will look gorgeous. If you're...

  • How to Get Rid of Mice In and Around The House?

    Until this year, the only time that I've had mice or rats in the house were when my daughter had pet mice and rats, and one of those times we did have one loose in the house. (Science fair project got loose.) Anyway,...

  • How to Build Wooden Roof Trusses

    You may refer to a truss as the rafter, but it's basically the skeleton of the roof, carrying the weight of the frame and supporting the walls of the building. Trusses are very important to preventing the walls from...

Click to Rate This Article