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.
ONLINE FORM SAMPLE:
The code below allows the user to select up to 5 files to upload to the server.
<cfform action="?uploadfiles=Yes&requesttimeout=600"
ENCTYPE="multipart/form-data"
method="post">
<center>
<table border=0 cellpadding=0 cellspacing=2>
<tr>
<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>
<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>
<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>
<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>
<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>
<tr>
<td colspan=2 valign=top class="standard-content" align="right"><input type="submit" value="Upload Files" style="FONT-SIZE: 10px"></td></tr>
</table>
</center>
</cfform>
PROCESS THE FORM SUBMISSION
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#"
NAMECONFLICT="Overwrite">
</cfif>
</cfloop>
</cfif>