【问题标题】:Coldfusion FileUpload function from cfc file not working来自 cfc 文件的 Coldfusion FileUpload 功能不起作用
【发布时间】:2013-05-08 14:07:41
【问题描述】:

我目前正在尝试使用 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 不包含文件。

【问题讨论】:

  • 要隔离问题,请在 .cfm 文件中运行 cffile 代码。

标签: file-upload coldfusion cfc cffile cffunction


【解决方案1】:

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


CFFILE 在 CFC 中上传

我不断看到人们对此感到困惑,所以这就是交易的内容。关于 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>

【讨论】:

  • 所以我应该在我的 CFC 文件中删除 arguments.fileToUpload 周围的井号?
  • 不,您应该在调用您的 cfc 时将它们从表单字段中删除 - 所以只需传递表单名称而不是值。
  • 这给了我错误:元素 FILETOUPLOAD 在 ARGUMENTS 中未定义。 (在第 7 行)` 6:`
  • 在调用 cfc 时,您仍然需要将表单字段名称作为参数放入,只是没有井号。它在 cfscript 而不是 cfml 中,但您可以在我提供的链接上看到它。
  • 非常感谢 完美运行,我永远感激不尽,谢谢 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多