Someone in #flex was talking about how there were no good examples for doing uploads with Flex and ColdFusion. Sounded like an excellent topic to cover here. Now this example is a tiny bit more complex than previous examples, because I needed to cover two methods of functionality: single file uploads and multi-file uploads. Both processes are very similar, and in my code they actually share a method.

Here’s the MXML application:

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" layout="absomWFR#Р׃67&CР4DDР'Bׂ6G&2W'CР&fFRf"הfTƗ7CfU&VfW&V6TƗ7CР&fFRf"הfSfU&VfW&V6SР&fFRf"WEF&vWCU$&WVW7BWrU$&WVW7B'WB6f"РР&fFRgV7FBfBРהfTƗ7BWrfU&VfW&V6TƗ7BРהfTƗ7BFDWfVDƗ7FVW"WfVB4TT5BfTƗ7E6VV7FVBРРהfRWrfU&VfW&V6RРהfRFDWfVDƗ7FVW"WfVB4TT5BfU6VV7FVBРРР&fFRgV7FfTƗ7D'&w6RfBРvRǒvBFrvW2F&RWFV@Рf"vW4fFW#fTfFW"WrfTfFW"$vW2""sVr"РהfTƗ7B'&w6RvW4fFW%ғРРР&fFRgV7FfT'&w6RfBРvRǒvBFrvW2F&RWFV@Рf"vW4fFW#fTfFW"WrfTfFW"$vW2""sVr"РהfR'&w6RvW4fFW%ғРРР&fFRgV7FfTƗ7E6VV7FVBSWfVBfBРW&RvR6VBFvFWfW"vRvB'WBvRw&RW7BvrFРWB&vBvРWDg&Ɨ7BРРР&fFRgV7FfU6VV7FVBSWfVBfBРW&RvR6VBFvFWfW"vRvB'WBvRw&RW7BvrFРWB&vBvРWDfRהfRРРР&fFRgV7FWDg&Ɨ7BfBРbהfTƗ7BfTƗ7BVwFРbFW&R&R7FfW2VgBFWB6FVPРהfRהfTƗ7BfTƗ7E2fU&VfW&V6SРWDfRהfRРV6RРbFW&R&R&RfW27FР&WGW&РРРРDSF2WFB2W6VBf"R"VFRfW0Р&fFRgV7FWDfRcfU&VfW&V6RfBРW&RvR'VBWBFRFW"f&FV0Рf"W&f'3U$f&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r"f6&S&f6R"Р׃d&У׃Ɩ6F

The comments point out all the nuances you need to know about. It does the major features, including passing more data with the file, what you can do with the progress event, how to do an upload queue, rather than uploading all selected files at the same time, and how to handle responses from the upload script. This code will work with any backend processor, not just ColdFusion.

Here’s the ColdFusion file:

<cfsetting enablecfoutputonly="true">
<!--- this will upload the file --->
<cffile action="upload" fileField = "myUploadFieldName" destination="#expandPath("./")#uploads/">

<!--- note that all formfields including FORM.myFirstValue and FORM.myOtherValue --->
<cfscript>
  myVal1 = FORM.myFirstValue;
  myVal2 = FORM.myOtherValue;
</cfscript>

<!--- here you can send stuff back to flex, only if the flash player is 9.0.28.0 or later --->
<cfoutput><myXML>
  <file uploadedAs="#cffile.serverFile#">
</myXML></cfoutput>

It doesn’t do much, but you can see how to get non-file data passed from flex, and how to send a response. Make sure that, when sending a response, you don’t send any whitespace before what you intend to send back to Flex. Also, note that your users need the Flash Player version 9.0.28.0 or later in order to send responses back to flex.

Help make me popular:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • Pownce
  • Slashdot
  • StumbleUpon

This entry was posted on Sunday, April 27th, 2008 at 3:18 pm and is filed under Syndicated. 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.

One Response to “Shan’s Simple Examples: File uploads with Flex and ColdFusion”

  1. Brian Rinaldi on July 7th, 2008 at 5:12 am

    Hey Shan, the MXML isn’t showing up right on the post.

Leave a Reply