3

現在、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 にはファイルが含まれていませんでした。

4

1 に答える 1

6

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>

arguments.fileField の周りのポンド記号ではありません。なんで?実際には文字列であるarguments.fileFieldの値を評価しているためです:「form.something」。使用例は次のとおりです。

<cfscript>
  fileObj = createObject('component','fileio');
  fileObj.fileUpload('form.something','c:\');
</cfscript>
于 2013-05-08T14:42:46.537 に答える