2013-05-08 42 views
2

我目前正在嘗試使用cfc文件中的函數上傳文件。當我在同一頁面上調用<cffile action="upload">時,我可以獲取代碼。不過,我想學習如何從cfc文件中的函數中調用它。我相信我傳遞給<cffile>標記的屬性存在一個問題,但我不確定。從cfc文件不工作的Coldfusion FileUpload功能

這裏是我的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 did not contain a file.

+0

要分離問題,請在.cfm文件中運行您的cffile代碼。 – 2013-05-08 14:45:01

回答

5

當使用CFFILE上傳,它需要一個表單字段的名字 - 你所傳遞的表單字段。請參閱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> 
+0

所以我應該刪除我的CFC文件中的arguments.fileToUpload的磅符號? – 2013-05-08 15:28:29

+0

不,你應該把它們從調用你的cfc的表單字段中移除 - 所以只需傳入表單名稱而不是值。 – Kristian82 2013-05-08 15:32:28

+0

這給了我錯誤: 元素FILETOUPLOAD在自變量中未定義。 (在線7) '6:' – 2013-05-08 15:42:12