Really Simple Flex File Download Example

I got a few requests asking how to download files from a server with Flex 2. In response I created this fast and dirty example to illustrate how it can be done. View Source is enabled and I have also commented the code to provide explanation to what is going on.

Related Blog Entries

Comments
Hi Ryan,

I'm VERY new to Flex and I've had a look at both your Upload and Download projects. Very cool!! I've never used CF before and I'm really not familiar with PHP. I've managed to create a PERL script to accept the files from your Upload Project and now I'm looking at tackling the Download piece. What output is the "Directory.cfm" producing? I'm hoping I can do this with PERL and pass it on to, I think, "Download.as"

Do you have any experience with PERL or can you tell me what the ouput of the "Directory.cfm" produces?

Thanks -- I look forward to your next installment!!!
# Posted By JohnEl | 11/30/06 6:17 PM
This example makes a service call to the "Directory.cfm" template via the HTTPService Flex Tag. The tag takes the output of Directory.cfm and returns its contents to Flex as a "result". In the case of the Directory.cfm template it loops through jpeg files in a directory and generates xml data instead of producing html. Think of Directory.cfm as an xml file that dynamically generates it's self each time it is call over http. In fact you can navigate to this with out Flex to see its output http://labs.newmediateam.com/FileDownload/service/... (if you browser displays a blank screen you'll have to view the source of the page).
# Posted By Ryan Favro | 12/1/06 12:36 PM
Well.. that looks dead easy to generate from PERL!!

Awesome
# Posted By JohnEl | 12/1/06 2:02 PM
Thats the beauty of Flex, its pretty much agnostic to what you use as a server side data source, thus you can host it just about anywhere. That said, if you're application needs every ounce of performance it can get I would suggest you look at using Coldufusion MX 7.02 to generate Flash Remoting calls as they are far lighter tiny binary packs. Flash Player parses these packs much more efficiently then it does XML or SOAP. Then of course there is FDS aka Flex Data Services but for a beginner get a handle on HTTP Service calls and or Flash Remoting First.
# Posted By Ryan Favro | 12/1/06 9:46 PM
Hello there -

I keep looking high and low, for a CFC example on how to perform a similar type of application - especially when it comes to *uploading* files to a server.

Are all the examples using CFM files, due to the fact that you have to use special URLEncoding to send Files - only possible when using CFM files?

My final goal, is to use RemoteObjects inside of Flex 2.0, in order to handle all my File Uploading tasks.

Am I stuck here, or does CF 7.02 now offer the ability to use CFC files when sending or receiving files?

Thanks in advance for your advice on this topic,

Mike
# Posted By Mike Anderson | 12/5/06 1:30 AM
Mike,

Look no further. First off you cannot to my knowledge upload to a RemoteObject such as a CFC. They way Flash Player handles files uploaded to the server is very similar to how an HTML Form would handle an upload. RemoteObjects use AMF Binaries to transport data and this doesn't jive with the way Flash Player can upload files to a server. You can still use a CFC to handle file uploads and perform any follow up actions to files delivered to the server. To do this simply call the CFC from within the CFM template that your application points to for uploads. Hopefully this clears up any confusion you may have on the subject.
# Posted By Ryan Favro | 12/5/06 9:41 AM
Hi Ryan,

I've been trying to get Flex and CF running under Apache 2.2 for almost 6 weeks, and your MultiFileUpload has become one of the 'validators' for me to see if it's actually working. So far, I've had no luck. I am running a completely fresh OS (XP Pro) and the entire system was just setup (I got frustrated and reformatted yet again last night), but upon trying to run MultiFileUpload, I receive error 2049 if I try externally and error 2038 if I try internally. I can't seem to upload anything. I've turned my firewall off the router and XP, I've even set permissions to the entire directory to full for everyone, and I've not enabled sandbox security in CF.

Am I supposed to be doing anything other than installing the software and going per your directions on the Adobe site? Is there something that is assumed that I might be unaware of?

I have ColdFusion accessing MySQL and MS Access with no problems (Mr. Camden's Blog) and the CF samples; I have the free FDS2 accessing the samples properly including the multi-player (hehe) dashboard and the sample crm (installed from this tutorial: http://www.brightworks.com/technology/brightworks_...), through Tomcat, but I can't get a simple upload to work here through Flex 2. I'm using it as an Eclipse Plug-In, if oyu've any suggestions, please.

I am trying to move over from Flash/PHP, not having much luck setting up a server though...

Any suggestions?

Shawn
# Posted By shawn | 12/23/06 11:58 AM
Just wanted to offer Ryan a huge public thanks for helping me out. As would be expected, the issues were with 'me' and not Ryan's work. He had me up and running (and knowing what I did wrong!) in no time.

Thanks again, and best in the New Year:)

Shawn
# Posted By shawn gibson | 12/30/06 8:12 PM
Great work Ryan Favro.
It is seen how many commentaries you in yours have blog.
Your excellent example of uploading with flex.
Made my learning to continue in front. Now this example to make donwloading with flex is in the same excellent line.
He will be possivel to place in the reply some lines of PHP as you made with the other example of uploading.
I come of Flash app (design and animation) I am to give the first steps in flex, and what I know of PHP is very little, but I go walking step by step. Very Tanks for yours posts
jf.sal
# Posted By jf.sal | 3/25/07 11:19 AM
Ryan,

I'm new to Flex 2.0 and have been looking at a better solution to developing GUI for my Java applications. I have a MultiFile Upload applet developed and it works. However, it does require the JRE which can turn customers away. What is the best way to configure Flex 2.0 to integrate with my servlets/beans? Am I looking at remoting services or a web service for this? I'm trying to keep this as simple as possible:-)
# Posted By Patrick | 6/19/07 4:05 PM
I have a question regarding the use of CFCs within a CFM page...

How do you use this method to return the new File.ServerFile name back to Flex? I'd like a way within Flex to show the picture that has just been uploaded.

Thanks.
# Posted By Marc | 8/13/07 1:20 AM
Hi,
this is my first run with Flex. I tried as you wrote in the readme.txt, but it doesn't work for me. Im running ColdFusion 8 Developer Edition on Windows 2003 Server with Apache 2.2. The CF Server runs on Port 80, but that can't be the problem.
# Posted By Patrick | 8/27/07 3:02 PM
Please concat with previous post.
The Flex App shows upload complete and doesn't throw any error. But it doesn't upload anything.
# Posted By Patrick | 8/27/07 3:16 PM
This flex demo is for downloading not uploading. Hence why it dosn't upload anything. Try this link http://ryanfavro.newmediateam.com/blog/index.cfm/2...
# Posted By Ryan Favro | 8/29/07 10:38 PM
# Posted By Allerfr | 3/17/08 3:52 AM
# Posted By zoloft | 3/27/08 1:40 AM
The download silently fails if your FileReference object is locally scoped. I tried to copy Ryan's syntax without copying his use of a FileReference object scoped to DownloadFile and I think I ran into the same problem as other commenters. Got the download box but no file was downloaded, no events were fired, no error was thrown. Found this bug report and solution on the Adobe site which fixed the problem.

http://kb.adobe.com/selfservice/viewContent.do?ext...
# Posted By lisa | 4/2/08 4:00 PM
Very good site, thanks !
http://www.voyance-officielle.fr
# Posted By voyance | 4/22/08 10:05 AM
Flex is cool
# Posted By bobg | 5/9/08 1:04 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.