Receiving data in response to file upload in Flex

I had to post this …was stumbled upon by my previous partner in crime at Optimal Payments (Dimitrios Gianninas). I’m not sure when this event was added but I could swear it was not there in 2.0. In any event, it’s there now! (2.01) Thanks to Darron Schall for pointing out that this was an addition the flash player rather than flex and requires minimum version of 9.0.28

Problem? After uploading data to a server via Flex there was no way to receive data back from the server in response to your upload. A common requirement. e.g. Upload a file for processing, server returns results.

Solution? Add a listener on your FileReference like so:

 

myFileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);

Your response handler would be something like:

 

private function responseHandler( event:DataEvent ) :void

{

var response:XML = XML( event.data );

}

Having the server return XML allows you to send a more complex response rather than straight text. In the example above simple manipulate the XML response as you see fit!

~ by Stacy Young on May 25, 2007.

12 Responses to “Receiving data in response to file upload in Flex”

  1. Very cool indeed, finally…

    Thnx!

  2. This isn’t a result of code added into Flex 2.0.1, but rather something that was added to the Flash Player.

    The uploadCompleteData event is only available in Flash Players 9,0,28,0+. You can add the listener regardless of the Flash Player version, but if you’re using this code in production then you should require that the user has at least the right minor revision number during Flash detection… otherwise the event will never fire.

  3. Thanks, great info. Will append to the post!

  4. Finally! Now I don’t need a database to keep track of the status of my uploaded files! Tnx for sharing!

  5. You just made my afternoon! I’ve been searching for this for hours…

  6. Oh yes exactly what I was looking for at 4 am :)
    thanks!

  7. Yeah, i found out too a few weeks ago,.. but i still got problems when sending files bigger than 5MB. The data comming back is totally ridiculous in this case… very strange..(heard of it on many posts now,.. if it’s a bug, hope they’re fixing it..)

    Guille

  8. almost decided tht it’s not gonna happen …. n.. i’ll have to find another way ….
    oh man u made my life easy ….
    thtz really fantastic …
    itz working ….

    hats off dude

  9. Awesome bit of information – has made things MUCH easier for me.
    Keep up the good work.

  10. Magnificent! Your solution is simple, works, and nicely described. Thank you so much for sharing this insight (and to Google for ranking it as the top search hit for “flex file upload response”). Keep on postin’, please!

  11. Super !!!
    You made my day… I stumbled across this problem and was looking through API with no such documentation. I was about to give and then thought of Googling and found your code snippet…

    Thanks for your help !!

  12. This really helps a lot! I’ve been looking for this solution for a long time! Thanks for the sharing.

Leave a Reply