Integrating Flex and Server-Side Scripts
Hi Everyone :]
I thought I would throw up some code I am working on, as I had a slight struggle with it, and I have a feeling that many of you would like to leverage uploading abilities within Flex applications.
I’m currently working on a web based mp3 player which uses Adobe AIR to communicate with iTunes self generated XML file. The flex application then loads the artists and allows users to select music which is then uploaded to the server simultaneously at the click of a button. This allows the users to “add” to their playlist without ever having to leave the application.
Although we have the fileReference class built in to the Actionscript API — It doesn’t technically handle the file uploading itself. I personally work with ColdFusion over other server technologies — so naturally that is the language my script was written in. I am working on a simple PHP script that does the exact same thing, but offers a free solution :]
Before writing the ColdFusion script all that is needed is a basic Flex form which in my case has a “browse button” that calls fileReference.browse() when clicked. Refer to the class reference for more information on the event handlers and such. Once a file(s) are selected, the user then clicks the “upload button” which calls the fileReference.upload(). This method requires one parameter, a URLRequest which should call your server side script file. You can also pass a second parameter to rename the file to that of you own naming conventions. This is very helpful if you want to archive uploaded files in a database, or an xml file. Finally, the upload() method has a third parameter which double checks that the file was uploaded by returning “true/false.”
As for the ColdFusion, only one tag is necessary:
<cffile action=”upload” filefield=”file” destination=”[Insert server destination here]” nameconflict=”overwrite>
That’s it! This method can be very helpful as you can allow users to upload their own videos , images, music, etc. The major benefit is it all happens within the same application so you can maintain the “feel.”
I personally have included an “upload” button in the mp3Player’s main state, which launches a popup window with the upload form :]. Have fun guys– See you all in April.
–Chris Bowden
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply