troubleshooting Question

How do I loop through a filecount and upload the correct number of images

Avatar of smfmetro10
smfmetro10Flag for United States of America asked on
ColdFusion Language
2 Comments1 Solution335 ViewsLast Modified:
Hi,

I have a form with the ability for the user to add file uploads to upload images etc.

It has a hidden file that keeps the filecount.

My problem is that on the upload page only one image is being uploaded to the correct folder.

I've included my code for better clarification

Thanks!
<!--EDIT PAGE-->
<script>
function addFile(theBtn){
  var theForm = theBtn.form;
  if(theForm.fileCount.value>=5) return;
  var fileNum = theForm.fileCount.value -= -1;
  var theDiv = theForm.userfile1;
  if(theDiv.length) theDiv = theDiv[0];
  theDiv = theDiv.parentNode;
  var newDiv = theDiv.cloneNode(true);
  var newFile = newDiv.getElementsByTagName("input")[0];
  newFile.name = "userfile"+fileNum;
  newFile.id = newFile.name;
  theBtn.parentNode.insertBefore(newDiv, theBtn)
}
</script>

<cfoutput><form action="insert.cfm?candidatenum=#url.candidatenum# method="post" enctype="multipart/form-data"></cfoutput>
<table>
<cfoutput>
<tr>
<td width="120">Project Name:</td>
<td width="317"><input type="text" name="txtProjectname1"  size="50"/></td>
</tr>
<tr>
<td>Project Description:</td><td><textarea name="txtProjectDesc1" cols="50" rows="5"></textarea></td>
</tr>
<tr>
<td>Work Plan:</td><td><input type="file" size="30" name="fleworkplan1" /></td>
</tr>
<tr>
<td>Work Samples:</td><td><input type="hidden" name="fileCount" value="1" />
<div>

<input type=file name="userfile1"  size="20" />
</div>

<input type=button value="Add" onClick="addFile(this)"></td>

</tr>

<tr>
<td><input type="submit" name="submit" value="Save and Review" /></td><td colspan="2"><input style="display:none" id="btnSubmit" type="submit" value="Submit for Evaluation" name="btnSubmit"><span style="display:inline; vertical-align:top" id="btnSubmitMsg">Check when you are done and ready to submit</span><input name="commission" type="checkbox" value="y" onclick="document.getElementById('btnSubmit').style.display=(this.checked?'inline':'none');document.getElementById('btnSubmitMsg').style.display=(this.checked?'none':'inline')"></td><td width="27" align="left">
<!---If yes, display an alert that they cannot submit and hide the button below. --->

</td>
</tr>
</cfoutput>
</table>


<!--INSERT PAGE -->

<cfif Form.submit eq "Save and Review">
<cfset thisDir = ExpandPath("./"& url.candidatenum)>
<cfif not directoryExists(thisDir )>
     <cfdirectory action="create" directory="#thisDir #">
</cfif>
<cfset thisDir = ("#thisDir#")>

<cfloop from="1" to="#FORM.fileCount#" index="counter">
      <!--- if this field contains a file ....--->
      <cfif len(form["userfile"& counter])>
            <!--- upload it to the proper folder....--->
            <cffile action="upload" 
                  fileField = "userfile#counter#" 
                 destination = "#thisDir#" 
                 nameConflict = "MakeUnique">
				 <cflocation url="candidate_show.cfm?candidatenum=#url.candidatenum#>				 </cfif></cfloop></cfif>
ASKER CERTIFIED SOLUTION
dgrafx

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros