Discussion:
Problem with Code
(too old to reply)
Abdlah
2009-03-27 11:54:21 UTC
Permalink
Passing the relevant URL variable to my form page, I am trying to allow the
user to upload multiple images to my site. I try to achieve this through making
my form submit to itself for processing after selecting the related files. For
ease of processing, I attempt to rename each of the uploaded files on the fly.

My problem is that I am finding it difficult to debug my code. I get an error
that [b]The form field XXX did not contain a file.[/b] even though I selected a
file.

Initially, I was checking if the form field is empty (now commented out), and
if so I was to ignore the upload, but I got an error that the field was not
defined - this I understood though, for I was created the field name of the fly
and making it dynamic, so I hadn't declared it earlier. [b]My question then is,
how do you solve the problem of declaring a variable that is created on the fly
dynamically?[/b]

I appreciate any help and sorry that my code long.

nb: my code is based on that of Charles Kaufmann which I got from devshed.com

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>eGhana - Upload Extra Images</title>
</head>

<body>

<!--- Check that the user is authenticated, otherwise send them to the login
page --->
<cfif not isDefined("SESSION.auth.isLoggedIn")>
<cflocation url="consumerLogin.cfm">
<!--- Create form for uploading extra images for product --->
</cfif>


<cfif IsDefined("URL.Cnsmr_ProductID")>
<cflock timeout=20 scope="Session" type="Exclusive">
<cfset Session.submitToSelf = 1>
</cflock>


<!--- Assign the productID to a session variable --->

<cflock timeout=20 scope="Session" type="Exclusive">
<cfset Session.ExtraProductImgName = URL.Cnsmr_ProductID>
</cflock>
</cfif>


<!--- ????????????????????????????????????????????????????? --->
<!--- CHECK THE LOGIC - product name must be active for both upload field
creation and actual upload process --->

<!-- make sure that Cnsmr_ProductID has been passed -->

<cfif not IsDefined("URL.Cnsmr_ProductID") and Session.submitToSelf eq 2 >
<!--- if it wasn't, then return to the consumers ad list --->
<cflocation url="consumerAd.cfm">

<cfelse>

<!---create session variable to hold number of form fields to create--->
<cflock timeout=20 scope="Session" type="Exclusive">
<cfset Session.numberoffields = 8>
</cflock>


<!--testing for the value of session variable -->
<cfdump var="#Session.ExtraProductImgName#">

<cfform action="uploadExtraConsumerProductImages.cfm"
enctype="multipart/form-data" method="post">
<cfloop index="i" from="1" to="#Session.numberoffields#" step="1">
<cfset filename = "#Session.ExtraProductImgName#" & #i#>
<!--- Set the respective file name for the uploaded image --->
<cfinput type="File"
name="<cfoutput>#variables.filename#</cfoutput>" /><br />
</cfloop>
<cfinput type="Submit" name="upload" value="upload" />
</cfform>

</cfif>

<cfif Session.submitToSelf eq 1 and isDefined("form.upload")>
<!--- ????????????????????????????????????????????????????? --->


<!---create variable to hold destination of uploaded files--->
<cfset upLoadDestination = "#ExpandPath('images/consumer/')#">
<cfif isdefined("form.upload")>
<cfloop index="i" from="1" to="#Session.numberoffields#" step="1">
<cfset filename = "#Session.ExtraProductImgName#" & #i#>
<!--- cfif evaluate(variables.filename) neq "" --->
<cffile action="UPLOAD" destination="#upLoadDestination#"
nameconflict="makeunique" filefield="#variables.filename#">
<!---/cfif--->
</cfloop>

<!--- Delete the session variable after uploading files related to this
particular ProductID --->
<cflock timeout=20 scope="Session" type="Exclusive">
<cfset StructDelete(Session, "ExtraProductImgName")>
</cflock>

<!--- disable resubmit flag after resubmitting --->
<cflock timeout=20 scope="Session" type="Exclusive">
<cfset Session.submitToSelf = 2>
</cflock>
</cfif>
</cfif> <!--- end extra upload processing --->

</body>
</html>
GArlington
2009-03-30 10:12:39 UTC
Permalink
Post by Abdlah
Passing the relevant URL variable to my form page, I am trying to allow the
user to upload multiple images to my site. I try to achieve this through making
my form submit to itself for processing after selecting the related files. For
ease of processing, I  attempt to rename each of the uploaded files on the fly.
 My problem is that I am finding it difficult to debug my code. I get an error
that [b]The form field XXX did not contain a file.[/b] even though I selected a
file.
 Initially, I was checking if the form field is empty (now commented out), and
if so I was to ignore the upload, but I got an error that the field was not
defined - this I understood though, for I was created the field name of the fly
and making it dynamic, so I hadn't declared it earlier. [b]My question then is,
how do you solve the problem of declaring a variable that is created on the fly
dynamically?[/b]
 I appreciate any help and sorry that my code long.
 nb: my code is based on that of Charles Kaufmann which I got from devshed.com
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <title>eGhana -  Upload Extra Images</title>
 </head>
 <body>
 <!--- Check that the user is authenticated, otherwise send them to the login
page --->
 <cfif not isDefined("SESSION.auth.isLoggedIn")>
     <cflocation url="consumerLogin.cfm">
    <!--- Create form for uploading extra images for product --->
 </cfif>
 <cfif IsDefined("URL.Cnsmr_ProductID")>
        <cflock timeout=20 scope="Session" type="Exclusive">
          <cfset Session.submitToSelf = 1>
     </cflock>
  <!--- Assign the productID to a session variable --->
     <cflock timeout=20 scope="Session" type="Exclusive">
          <cfset Session.ExtraProductImgName = URL.Cnsmr_ProductID>
     </cflock>
 </cfif>          
 <!--- ????????????????????????????????????????????????????? --->
 <!--- CHECK THE LOGIC - product name must be active for both upload field
creation and actual upload process --->
    <!-- make sure that Cnsmr_ProductID has been passed -->
    <cfif not IsDefined("URL.Cnsmr_ProductID") and Session.submitToSelf eq 2 >
    <!--- if it wasn't, then return to the consumers ad list --->
    <cflocation url="consumerAd.cfm">
         <cfelse>
        <!---create session variable to hold number of form fields to create--->
        <cflock timeout=20 scope="Session" type="Exclusive">
          <cfset Session.numberoffields = 8>
     </cflock>
    <!--testing for the value of session variable -->
    <cfdump var="#Session.ExtraProductImgName#">
          <cfform action="uploadExtraConsumerProductImages.cfm"
enctype="multipart/form-data" method="post">
         <cfloop index="i" from="1" to="#Session.numberoffields#" step="1">
           <cfset filename = "#Session.ExtraProductImgName#" & #i#>
                  <!--- Set the respective file name for the uploaded image --->
Your code is VERY difficult to read, BUT your problem is here...
Post by Abdlah
            <cfinput type="File"
name="<cfoutput>#variables.filename#</cfoutput>" /><br />
You do NOT need <cfoutput>...</cfoutput> inside cfinput tags...
Post by Abdlah
                </cfloop>  
            <cfinput type="Submit" name="upload" value="upload" />
         </cfform>
    </cfif>
 <cfif Session.submitToSelf eq 1 and isDefined("form.upload")>
  <!--- ????????????????????????????????????????????????????? --->
 <!---create variable to hold destination of uploaded files--->
 <cfset upLoadDestination = "#ExpandPath('images/consumer/')#">
     <cfif isdefined("form.upload")>
       <cfloop index="i" from="1" to="#Session.numberoffields#" step="1">
           <cfset filename = "#Session.ExtraProductImgName#" & #i#>
             <!--- cfif evaluate(variables.filename) neq "" --->
                  <cffile action="UPLOAD" destination="#upLoadDestination#"
nameconflict="makeunique" filefield="#variables.filename#">
                 <!---/cfif--->
          </cfloop>
           <!--- Delete the session variable after uploading files related to this
particular ProductID --->
           <cflock timeout=20 scope="Session" type="Exclusive">
             <cfset StructDelete(Session, "ExtraProductImgName")>
        </cflock>
     <!--- disable resubmit flag after resubmitting --->
        <cflock timeout=20 scope="Session" type="Exclusive">
          <cfset Session.submitToSelf = 2>
     </cflock>
     </cfif>
 </cfif> <!--- end extra upload processing --->
 </body>
 </html>
Loading...