Skip to main content
nikos101
Inspiring
June 21, 2010
Answered

How does one upload a file from flex to a cfc

  • June 21, 2010
  • 3 replies
  • 3837 views

How does one upload a file from flex to a cfc?   Can you do the file upload via remote object? I usually do uploads cia cfm but prefer the asnc capabilities of remote object calls.

This topic has been closed for replies.
Correct answer jfb00

Hi,

The issue here is myFile. For flex we need to pass this name as paramenter becuase it's not html form item like:

<cfcomponent>

    <cffunction name="uploadFile" access="remote" output="false" returntype="void">

    <cfargument name="file" required="Yes" type="any">
        <cffile action="upload" filefield="#arguments.file#" destination="C:\temp\" nameconflict="makeunique">
    </cffunction>
</cfcomponent>

From flex I'm getting the file using fileReference like:

private function browseAndUpload():void
            {
                fileReference = new FileReference();
                fileReference.addEventListener(Event.SELECT,fileReference_Select);
                fileReference.addEventListener(Event.CANCEL,fileReference_Cancel);
                fileReference.browse();
            }
           
            private function fileReference_Select(event:Event):void
            {
                fileReference.addEventListener(ProgressEvent.PROGRESS,fileReference_Progress);
                fileReference.addEventListener(Event.COMPLETE,fileReference_Complete);
                fileReference.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);
               
                //fileReference.upload(request);
                fileName = fileReference.name;
                myFile.htmlText = fileReference.name;
                SM_RO.importNSA();
            }

filerReference have an upload method to upload the file but I can't use that becuase I need to process my upload with my cfc.

I'm getting an error:

Failed to import the file. The cffile action="upload" requires forms to use enctype="multipart/form-data". Please also make sure the file is not open.

I can't find the way to setup the enctype becuase again i can have html form items in flex, or I can?

Thanks

Johnny

3 replies

New Participant
February 8, 2011

Hi all

Is it possible to upload a file with a cfc from flex, if some can some one please post some code.

Thanks Asen

Muzak
Inspiring
February 8, 2011
Inspiring
October 4, 2010

Will something like this not work:

<cfcomponent>

    <cffunction name="uploadFile" access="remote" output="false" returntype="void">
        <cffile action="upload" filefield="myFile" destination="C:\temp\" nameconflict="makeunique">
    </cffunction>
   
</cfcomponent>

(where myFile is the name of the formfield containing the uploaded file's name).

This works fine from an HTML-originated POST, so I would assume it would work from a Flash-originated POST too: the CF server won't care (or even know).

Also, I'm intrigued by what you mean by the "asnc capabilities of remote object calls" comment.  Just that I don't understand what you mean, I mean.  I'm not questioning it.

--

Adam

jfb00Correct answer
Inspiring
October 4, 2010

Hi,

The issue here is myFile. For flex we need to pass this name as paramenter becuase it's not html form item like:

<cfcomponent>

    <cffunction name="uploadFile" access="remote" output="false" returntype="void">

    <cfargument name="file" required="Yes" type="any">
        <cffile action="upload" filefield="#arguments.file#" destination="C:\temp\" nameconflict="makeunique">
    </cffunction>
</cfcomponent>

From flex I'm getting the file using fileReference like:

private function browseAndUpload():void
            {
                fileReference = new FileReference();
                fileReference.addEventListener(Event.SELECT,fileReference_Select);
                fileReference.addEventListener(Event.CANCEL,fileReference_Cancel);
                fileReference.browse();
            }
           
            private function fileReference_Select(event:Event):void
            {
                fileReference.addEventListener(ProgressEvent.PROGRESS,fileReference_Progress);
                fileReference.addEventListener(Event.COMPLETE,fileReference_Complete);
                fileReference.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);
               
                //fileReference.upload(request);
                fileName = fileReference.name;
                myFile.htmlText = fileReference.name;
                SM_RO.importNSA();
            }

filerReference have an upload method to upload the file but I can't use that becuase I need to process my upload with my cfc.

I'm getting an error:

Failed to import the file. The cffile action="upload" requires forms to use enctype="multipart/form-data". Please also make sure the file is not open.

I can't find the way to setup the enctype becuase again i can have html form items in flex, or I can?

Thanks

Johnny

Inspiring
October 4, 2010

I'm making this up as I go along a bit, but I would not worry so much about the CF error message saying the form needs to have that enctype.  What it means is the HTTP request needs to have a content-type of multipart/form-data.  It doesn't matter how it gets to be sent like that... a form-tag attribute, or just setting the header via some other mechanism: neither the web server nor CF will know/care how it goes done.


A case in point is that one doesn't need to post to that CFC method via a form... one could just as much post if via <cfhttp>.  There's no forms involved in doing it that way; just CF creating an HTTP request.

Now... I don't know Flash from... err... Adam, but I'm "certain" that if CF received the request in the correct format (so the corrent content-type, and a field with a file in it), the CF side of things would work fine.

It's just a matter of coercing Flex to do that for you with Flash.  Which might be a question for a Flex forum, not a CF one..?  The question, I think, is more "how do I upload a file using Flex" rather than "... to a CF server".

--

Adam

Inspiring
October 4, 2010

HI,

I'm tryin to do this now, did you figure this out?

thanks

Johnny

nikos101
nikos101Author
Inspiring
October 4, 2010

nope