2012-01-04 2 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. 양식 호출에서 직접 업로드 방법을 호출하기 때문에 액세스 유형은 이어야합니다 (원격).
  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>`