CFfile - 未将值设置为查询数据

时间:2011-01-06 22:03:45

标签: coldfusion file uploading cfc cffile

我有这个添加用户表单,它还可以通过查询数据并设置value =“#query.xvalue#”来兼作编辑用户表单。如果用户存在(例如,您正在编辑用户,则会从数据库中加载用户数据。在<cffile字段上执行此操作时,它不会加载数据,然后在插入插入时数据用空字符串覆盖数据库值(如果用户没有输入新文件)。我该如何避免这种情况?

代码:

形式:

<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>        

表格处理器:

<!--- 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">   

查询:

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

1 个答案:

答案 0 :(得分:2)

  

&lt; cfif not   len(Trim(form.cprAdultImage))EQ 0&gt;

这实际上与你想要的相反。我相信你想说的是:

<!--- 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>

修改 顺便说一下,因为你的所有参数都是可选的,所以代码感觉有点尴尬。

  

&lt; cfinput type =“file”required =“no”name =“cprAdultImage”value =“#certificationsList.cprAdultImage#”&gt;,

您无法真正预设文件控件“值”(浏览器安全限制)。由于用户可以在两种情况下上传文件,只需提供输入并仅在存在先前图像时显示“查看”链接。

<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)#',

UPDATE逻辑看起来也有点偏。如果未提供文件,您将最终用空字符串覆盖现有图像。为避免这种情况,仅在提供新文件时更新该字段(即非空)

表格处理:

<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="">

查询

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 ....

不要忘记清理。删除所有旧文件...