Search code examples
coldfusionfileuploadingcfccffile

CFfile -- value is not set to the queried data


I have this add user form, it also doubles as a edit user form by querying the data and setting the value="#query.xvalue#". If the user exists (eg, you're editing a user, it loads in the users data from the database. When doing this on the <cffile field it does not load in the data, then when the insert goes to insert data it overrights the database values with a blank string (If a user does not input a new file). How do I avoid this?

Code:

Form:

<br/>Digital Copy<br/>
<!--- If null, set a default if not, set the default to database default --->
<cfif len(Trim(certificationsList.cprAdultImage)) EQ 0>
 <cfinput type="file" required="no" name="cprAdultImage" value="" >
 <cfelse>
    File Exists: <cfoutput><a href="#certificationsList.cprAdultImage#">View File</a></cfoutput>
    <cfinput type="file" required="no" name="cprAdultImage"  value="#certificationsList.cprAdultImage#">
</cfif>        

Form Processor:

<!--- Has a file been specificed? ---> 
<cfif not len(Trim(form.cprAdultImage)) EQ 0>
    <cffile action="upload" filefield="cprAdultImage" destination="#destination#" nameConflict="makeUnique">
    <cfinvokeargument name="cprAdultImage" value="#pathOfFile##cffile.serverFile#">
<cfelse>
    <cfinvokeargument name="cprAdultImage" value="">
</cfif>

CFC

ARGS:

<cfargument name="cprAdultExp" required="NO">
<cfargument name="cprAdultCompany" type="string" required="no">
<cfargument name="cprAdultImage" type="string" required="no">
<cfargument name="cprAdultOnFile" type="boolean" required="no">   

Query:

UPDATE mod_StudentCertifications
SET 
cprAdultExp='#DateFormat(ARGUMENTS.cprAdultExp, "mm/dd/yyyy")#',
cprAdultCompany='#Trim(ARGUMENTS.cprAdultCompany)#',
cprAdultImage='#Trim(ARGUMENTS.cprAdultImage)#',
cprAdultOnFile='#Trim(ARGUMENTS.cprAdultOnFile)#'

INSERT INTO 
mod_StudentCertifications(
cprAdultExp,
cprAdultcompany,
cprAdultImage,
cprAdultOnFile

Solution

  • <cfif not len(Trim(form.cprAdultImage)) EQ 0>

    That is actually doing the reverse of what you want. I believe what you are trying to say is:

    <!--- if the length is greater than 0, assume a file was uploaded --->
    <cfif len(Trim(form.cprAdultImage)) GT 0>
         file is not empty. do the upload ...
         <cfinvokeargument name="cprAdultImage" value="#pathOfFile##cffile.serverFile#">
    <cfelse>
        <cfinvokeargument name="cprAdultImage" value="">
    </cfif>
    

    Edit As an aside, .. since all of your arguments are optional, the code feels a bit awkward.

    <cfinput type="file" required="no" name="cprAdultImage" value="#certificationsList.cprAdultImage#">,

    You cannot really preset the file control "value" (browser security restrictions). Since a user can upload a file in both cases, just provide an input and display the "view" link only if a previous image exists.

    <cfif len(Trim(certificationsList.cprAdultImage))>
        File Exists: 
        <cfoutput><a href="#certificationsList.cprAdultImage#">View File</a></cfoutput>
    </cfif>        
    <cfinput type="file" required="no" name="cprAdultImage">
    

    cprAdultImage='#Trim(ARGUMENTS.cprAdultImage)#',

    The UPDATE logic looks a bit off as well. If a file was not supplied, you will end up overwriting the existing image with an empty string. To avoid that, only update that field if a new file was supplied (ie non-empty)

    Form processing:

    <cfif len(Trim(form.cprAdultImage)) GT 0>
        <cffile action="upload" filefield="cprAdultImage" destination="#destination#" nameConflict="makeUnique">
        <cfinvokeargument name="cprAdultImage" value="#pathOfFile##cffile.serverFile#">
    </cfif>
    

    CFC ARGS:

    <!--- if values are always used in queries, should be required=true --->
    <cfargument name="cprAdultExp" required="yes">
    <cfargument name="cprAdultCompany" type="string" required="yes">
    <cfargument name="cprAdultOnFile" type="boolean" required="yes">   
    <cfargument name="cprAdultImage" type="string" default="">
    

    Query

    UPDATE mod_StudentCertifications
    SET 
    cprAdultExp='#DateFormat(ARGUMENTS.cprAdultExp, "mm/dd/yyyy")#',
    cprAdultCompany='#Trim(ARGUMENTS.cprAdultCompany)#',
    <!--- only overwrite the value if a new file was supplied --->
    <cfif len(trim(ARGUMENTS.cprAdultImage))>
       cprAdultImage='#Trim(ARGUMENTS.cprAdultImage)#',
    </cfif>
    cprAdultOnFile='#Trim(ARGUMENTS.cprAdultOnFile)#'
    WHERE ....
    

    And do not forget cleanup. Remove any old files...