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>

More by this Author

  • Gun Laws in Georgia
    38

    Firearms can be a very dangerous tool in the hands of the wrong person. Every state has their own gun laws and regulations. For the state of Georgia, the laws are pretty straight forward and easy to abide by. Before you...

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

    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
    23

    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
working