Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Location
    Houston, TX
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Uploading multiple files

    I keep getting this error: Element NUMBEROFFIELDS is undefined in VARIABLES

    If anyone could help I would appreciate it.

    Here is the client side:
    <cfset numberoffields = 4>
    <form action="fileupload.cfm" method="post" enctype="multipart/form-data">
    <cfloop index="i" from="1" to="#numberoffields#" step="1">
    <cfset filename = "file" & #i#>

    <input type="file" name="<cfoutput>#filename#</cfoutput>" /><br />

    </cfloop>

    <input type="submit" name="submit" value="Upload">
    </form>

    Here is the server side:
    <cfloop index="i" from="1" to="#variables.numberoffields#" step="1">
    <cfset filename = "form.file" & #i#>

    <cfif evaluate(variables.filename) NEQ "">

    <cffile action="upload" filefield="#variables.filename#" destination="d:\bla\bla\bla" accept="image/jpg, image/png, image/x-png, application/msword, application/vnd.ms-word, application/mspowerpoint, application/vnd.ms-powerpoint, application/pdf, application/msexcel, application/vnd.ms-excel" nameconflict="makeunique">
    </cfif>
    </cfloop>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder
    Join Date
    Feb 2009
    Location
    NJ, USA
    Posts
    476
    Thanks
    2
    Thanked 70 Times in 69 Posts
    Hey Rick. What the problem is, is that you're not actually sending the numberOfFields variable in the form, and therefore it's not getting to the next page (fileUpload.cfm). Your best option is to send numberOfFields in a hidden form field like this:

    Code:
    <cfset numberOfFields = 4>
    <form action="fileUpload.cfm" method="post" enctype="multipart/form-data">
      <input type="hidden" name="numberOfFields" value="#numberOfFields#">
    
      <cfloop index="i" from="1" to="#numberoffields#" step="1">
         <cfset filename = "file" & #i#>
    
         <input type="file" name="<cfoutput>#filename#</cfoutput>" /><br />
       </cfloop>
    
      <input type="submit" name="submit" value="Upload">
    </form>
    The only variables that the fileUpload.cfm page are going to receive are the variables from the form's submission (and URL variables, if you sent any). That is, it doesn't matter what variables you set with <cfset> on the form's page, they have no way of finding themselves to the fileUpload.cfm page when the form is submitted unless they are actually a form field.

    Now on your fileUpload.cfm page, variables that are coming from the form should be referenced in the form scope. You have the filename one correct, but your loop should be:
    Code:
    <cfloop index="i" from="1" to="#form.numberOfFields#">
    (By the way, you don't need to specify the step="1" attribute, as 1 is the default step.)


    Real quick just to let you know, any document with ColdFusion code in it is really "server side" code. ColdFusion code is always run at the server, and then the user's browser just receives the resulting HTML page that is created by running that code. So even tho your code looked like this in the .cfm file for the code that you labeled as the "client side" code:
    Code:
    <cfset numberOfFields = 4>
    <form action="fileUpload.cfm" method="post" enctype="multipart/form-data">
      <cfloop index="i" from="1" to="#numberoffields#" step="1">
         <cfset filename = "file" & #i#>
    
         <input type="file" name="<cfoutput>#filename#</cfoutput>" /><br />
       </cfloop>
    
      <input type="submit" name="submit" value="Upload">
    </form>
    The code that the client's browser actually recieved is:
    Code:
    <form action="fileupload.cfm" method="post" enctype="multipart/form-data">
      <input type="file" name="file1" /><br />
      <input type="file" name="file2" /><br />
      <input type="file" name="file3" /><br />
      <input type="file" name="file4" /><br />    
      <input type="submit" name="submit" value="Upload">
    </form>
    You can always check what the client's browser receives by right clicking on your own browser and clicking "View Source" in IE, or "View Page Source" in Firefox.

    Hope that helps, and post again if there's anything else.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •