tags:

views:

819

answers:

4

I'm a complete Flex noob, so I apologize in advance if I'm missing something obvious.

I wrote a fairly simple file uploader in Flex, which calls my Django back-end via URLRequest (the FileReference object handles the upload). My upload works as intended and I have Django return a HTTPResponse object. As such, I'd like to read the contents of the HTTPResponse object.

Any thoughts?

+2  A: 

By the time it gets to the client it's just a normal HTTP response so treat it like any other response

andybak
+2  A: 

something along the lines of

<mx:HTTPService id="myHTTPRequest" 
    url="{whatever your url request is}"
    result="resultHandler(event)" 
    fault="faultHandler(event)"
    showBusyCursor="true" 
    resultFormat="object">

then inside the resultHandler something like this

private function resultHandler (event : ResultEvent) : void {
    var obj : Object = event.result;
    //do something with returned object

}

Debug at the point of the resultHandler to see exaclty whats being returned, make sure its what you think should be getting returned.

kenneth
I'd like to go this route, but FileReference.upload() expects a URLRequest as the first parameter. I've tried using "as URLRequest", but no luck. Any advice?
Huuuze
hmm thats quite a different thing then. I'm pretty sure that you can't get at the response. If you use a HTTP service like above then fine, if your using the FileReference then all you can get is the events such as complete, progress, IO_error etc.
kenneth
Could you on the complete event from the fileReference then create and send a HTTP request to get whatever your sending back? what data do you want back, the upload should just be a upload and not a upload and return data.
kenneth
A: 

you can access the response like so in your onComplete event handler:

private function saveCompleteHandler(event:Event):void {
    var loader:URLLoader = event.currentTarget as URLLoader;
    trace("saveCompleteHandler - event returned:" + loader.data as String);
}

we do this this to get json fron a java web service.

you just need to use a URLLoader to load the URLRequest in the first place:

var loader:URLLoader = new URLLoader();
loader.addEventListener(HTTPStatusEvent.HTTP_STATUS, statusHandler, 10000);
loader.addEventListener(IOErrorEvent.IO_ERROR, saveErrorHandler, 10000);
loader.addEventListener(Event.COMPLETE, saveCompleteHandler, 10000);

var request:URLRequest = new URLRequest("http:/whereverer");
request.method = URLRequestMethod.GET;
loader.load(request);
Kieran H
+1  A: 

Hi, I am also new to flex and I ran in the same problem when doing an upload to a Java Rest backend, I solved it using the DateEvent on the FileReference. To get the response data use something like this.:

var fileRef:FileReference = new FileReference();
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);
var request:URLRequest = new URLRequest("yourUrl");
fileRef.upload(request, "fileData"); 

private function responseHandler(event:DataEvent):void {
    var response:XML = new XML(event.data); 
//Note the DataEvent: this is the event that holds the response. 
//I sent back data as xml
}

Your response should always be a successful HTTP status code (200), if your backend sends status 500 codes it will not trigger the DateEvent. Server errors can still be caught with a HTTPStatusEvent, but then you don't have access to the response.

PD