Receiving data in response to file upload in Flex using FileReference

I don’t know how much you have faced this problem and were unable to figure out the way to do it. Today I had this problem and I found the solution and wanna share this with all of you. So here it is,

My problem was after uploading data to a server via Flex i.e. FileReference.upload,  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 (typically status).

Solution: Add a listener on your FileReference object: (Make sure you have flash player build version greater than 9.0.28). To check this right click on the Flash application opened in Bowser and choose about.

fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, responseHandler);

private function responseHandler( event:DataEvent ) :void
{
var response:XML = XML( event.data );
Alert.show(response.toString())
}

Have fun :)

6 Responses to “Receiving data in response to file upload in Flex using FileReference”

  1. Tony Fendall Says:

    It’s a pitty that this requires the user to have the latest version of the flash player, but it is really convenient.

  2. kailas R. Deokar Says:

    hi Anand,
    This is kailas Deokar, i want a brif example to upload the local file on to server.Plz help me to achieve this.

  3. Chee Beng Says:

    Very nice of you to share this with us.

    May I ask whether do you have a solution for FileReference to return the path? Here is my problem in more details.

    I want to upload a file to server. FileReference.browse() is doing a fine job allowing user to select a file. However, I would like to gain access to the path for some reasons. It is a pitty FileReference seems like it is not able to do that!

    Thanks.

  4. Anand Says:

    You can never get local path from client due to security reasons. but if you are working in AIR it provides you documentLocalPath passed with file object.

  5. Umer Khan Says:

    hey?
    can you give me a complete code or ne sample how to use this filereference. i got many example but they all need servelt? do i need a servlet too? let me know how to do it?

  6. Razvan Pat Says:

    Thank you, you just saved me from a lot of headaches. :)

Leave a Reply