2012-01-04 7 views
1

私は間違っていますか?オブジェクト内のファイルをアップロード

fileUpload.cfm

<cfcomponent name="fileAttachment" hint="This is the File Attachment Object"> 

    <cffunction name="uploadFile" access="public" output="no" returntype="string"> 
     <cfargument name="fileToUpload" type="string" required="no"> 
     <cfargument name="pDsn" required="no" type="string"> 
     <cfset var cffile = ""> 
     <cffile action="upload" destination="D:\apache\htdocs\abc\uploads" filefield="#ARGUMENTS.fileToUpload#" nameconflict="makeunique"> 
     <cfreturn cffile.clientFile /> 
    </cffunction> 

</cfcomponent> 

test_fileUpload.cfm

<form action="fileUpload.cfm" enctype="multipart/form-data" method="post"> 
    <input type="file" name="fileToUpload"><br/> 
    <input type="submit"> 
</form> 

答えて

2

このライン:

<cffile action="upload" destination="D:\apache\htdocs\abc\uploads" filefield="#ARGUMENTS.fileToUpload#" nameconflict="makeunique"> 

FileFieldに属性がの名前を望んでいますアップロードされたファイルを保持するフォームフィールドあなたは正しい軌道に乗っていますが、残念ながら#ARGUMENTS.fileToUpload#の値ではありません。現時点では、実際のファイルそのものへの参照を保持しています。 、そして、

<input type="hidden" name="nameOfField" value="fileToUpload"> 

最初のパラメータとしてあなたuploadFile()メソッドにFORM.nameOfFieldを渡す:

は、フォームに新しい隠しフィールドを追加します。 CFFILEは残りの部分を処理します。

0

まあ、私はこのコードで多くの問題を発見しました。

  1. fileupload.cfmは、コンポーネントファイルfileuploadである必要があります。 cfcと書いてあります。
  2. フォームコールから直接アップロードメソッドを呼び出すため、アクセスタイプはREMOTEである必要があります。
  3. アクションページはfileupload.cfc?method = uploadFileに変更する必要があります
  4. cffileをコンポーネントのローカル変数として定義すると、cffileタグにresult = "cffile"属性を指定する必要があります。
  5. filefield属性はその値ではなくフォームフィールドの名前をとるので、##タグだけを削除してfileToUploadを使用してください。

以下は正しいコードです。 fileupload.cfc

<cffunction name="uploadFile" access="remote" output="no" returntype="string"> 
    <cfargument name="fileToUpload" type="string" required="no"> 
    <cfargument name="pDsn" required="no" type="string"> 
    <cfset var cffile = ""> 
    <cffile action="upload" destination="D:\apache\htdocs\abc\uploads" filefield="fileToUpload" result="cffile" nameconflict="makeunique"> 
    <cfreturn cffile.clientFile /> 
</cffunction> 

</cfcomponent> 

test_fileupload.cfm

`<form action="fileupload.cfc?method=uploadFile" enctype="multipart/form-data" method="post"> 
    <input type="file" name="fileToUpload"><br/> 
    <input type="submit"> </form>` 
関連する問題