cfc文件中的Coldfusion FileUpload函数无法正常工作

时间:2013-05-08 14:07:41

标签: file-upload coldfusion 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> 

以下是表单提交到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 

以下是我的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>

上传文件时我一直收到的错误是:

  

neotmp84939430443.tmp不包含文件。

1 个答案:

答案 0 :(得分:5)

使用cffile上传时,需要一个表单字段名称 - 您传入的是表单字段 value 。请参阅this link了解实现目标的确切方法。


在CFC中上传CFFILE

我一直看到人们被绊倒了,所以这就是交易的目的。关于cffile上传的最重要的事情是,无论如何,文件字段都有作为您尝试上传的表单字段的名称。

示例:

注意缺少英镑迹象。这意味着你正在强迫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>

不是英镑在arguments.fileField周围签名。为什么?因为你正在评估arguments.fileField的值,它实际上是一个字符串:'form.something'。示例用法是:

<cfscript>
  fileObj = createObject('component','fileio');
  fileObj.fileUpload('form.something','c:\');
</cfscript>