Err*_*ace 2 coldfusion file-upload cfc cffile cffunction
我目前正在尝试使用cfc文件中的函数上传文件.当我<cffile action="upload">在同一页面上调用时,我可以让代码工作.但是,我想学习如何从cfc文件中的函数调用它.我相信我传递给<cffile>标签属性的参数存在问题,但我不确定.
这是我的HTML表单:
<form action="confirm.cfm" method="POST" enctype="multipart/form-data">
First Name: <input type="text" name="FNAME" size="25" /> <br />
Last Name: <input type="text" name="LNAME" size="30" /> <br />
Upload Attachment File Here: <input type="file" name="fileUpload"
id="fileUpload" size="30"
onchange="PassFileName()" /> <br />
<input type="hidden" name="fileName" id="fileName" /> <br />
<input type="submit" value="submit" />
<input type="reset" name="Reset Form" />
<div id="filenamesection">
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
以下是表单提交到confirm.cfm页面的代码
<cfif isDefined("Form.fileUpload")>
<cfinvoke component="FileUploadExample"
method="UploadFile"
fileToUpload="#Form.fileUpload#"
sizeMax="50"
returnvariable="FileNameReturn">
<cfelse>
<cflocation url="index.cfm">
</cfif>
<h1> Thank for filling out this form </h1>
<cfoutput>
<table>
<h3> Please review and confirm the information below </h3>
<tr>
<td> is this the first name you entered </td>
<td> #Form.FNAME# </td>
</tr>
<tr>
<td> is this the last name you entered </td>
<td> #Form.LNAME# </td>
</tr>
<tr>
<td> is this the name of the file you uploaded? </td>
<td> #Form.fileName# </td>
</tr>
</table>
</cfoutput>
The File <cfoutput> #Form.fileName# </cfoutput> has been uploaded
Run Code Online (Sandbox Code Playgroud)
这是我的FileUploadExample.cfc文件中的代码
<cfcomponent>
<cffunction name="uploadFile" access="public" output="no" returntype="string">
<cfargument name="fileToUpload" type="string" required="no">
<cfset var cffile="">
<cffile action="upload"
destination="C:\filepath\example\whatevaFolder\"
filefield="#ARGUMENTS.fileToUpload#"
nameconflict="overwrite">
<cfreturn cffile.clientFile>
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
我上传文件时一直收到的错误是:
neotmp84939430443.tmp不包含文件.
当使用cffile上传时,它需要一个表单字段名称 - 您传入的是表单字段值.请参阅此链接以获取实现您要执行的操作的确切方法.
CFFILE在CFC中上传
我一直看到人们被绊倒,所以这就是交易的目的.你必须要记住CFFILE的上传最重要的事情是,不管是什么,FileField或有是你要上传表单字段的名称.
例:
注意缺少英镑的迹象.这意味着你正在强迫cffile去寻找form.something并从中获取数据流.那么,如果将它放在CFC中,你需要做什么?您无法将数据流传递到CFC(您可以,但这不是正确的方法).您必须将文件字段的名称传递给cfc.
示例CFC:
<cfcomponent name="fileio">
<cffunction name="fileUpload" access="public" returntype="struct">
<cfargument name="fileField" required="true" type="string">
<cfargument name="destination" required="true" type="string">
<cffile action="upload" filefield="#arguments.fileField#" destination="#arguments.destination#">
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
不是英镑在arguments.fileField周围签名.为什么?因为你正在评估arguments.fileField的值,它实际上是一个字符串:'form.something'.示例用法是:
<cfscript>
fileObj = createObject('component','fileio');
fileObj.fileUpload('form.something','c:\');
</cfscript>
Run Code Online (Sandbox Code Playgroud)