Macromedia Flash Player 8 is here and packed with useful and innovative features. Performance and memory consumption are also significantly improved in the new player, and you will notice the improvement without changing a line of code or recompiling.
As a Flex developer, you will probably spot immediate opportunities to use some of the new features in Flash 8:
So, Flash Player 8 provides a lot of great features. How do you take advantage of these features in Flex 1.5? This article explains how you can take advantage of the new features in Flash Player 8 by building content for it using the Flash 8 authoring tool and then loading the resulting SWF file into a Loader component in your Flex application. The process is simple and straightforward. You continue to build all your application interface and logic in Flex and simply leverage a small library of Flash Player 8 functions. Better yet, for the file upload and download feature, we provide a code example in this article that you can drop into your Flex 1.5 applications without needing to use the Flash authoring tool.
To complete this tutorial you will need to install the following software and files:
As of the writing of this article, Flash 8 is not yet available. When it becomes available, you will be able to use it to extend the example provided here and create Flash 8 content to integrate into your Flex applications. Find out more about the Flash 8 authoring tool.
Flash Player 8 delivers a host of new features and capabilities. While it’s tempting to access the APIs directly in ActionScript and compile and run your Flex 1.5 application leveraging those features, this won’t work for several reasons. Primarily, the Flex 1.5 compiler won’t recognize the new ActionScript statements. It is hard-coded to produce Flash Player 7 SWF files. An upcoming release of Flex will give full support for Flash Player 8.
Don’t despair, however, you can still leverage many of the features in Flash Player 8 using a different method. First, you can build small blocks of Flash Player 8 SWF files using the Flash 8 authoring tool, producing libraries of Flash Player 8 classes that you can use in your Flex 1.5 applications. Then, you can load Flash Player 8 SWF files into your Flex 1.5 applications using a Loader component and gain access to them in your Flex 1.5 application through ActionScript. The approach is simple and gives you access to many of the Flash Player 8 features most requested by Flex developers.
There are a couple of ways of achieving this. You can create Flash Player 8 SWF files that have no user interface and just provide a set of classes and methods to use in your Flex 1.5 application. Or, you can build whole interfaces in Flash Player 8 content that display and run in your Flex application. Regardless of the approach you choose, you can communicate events, properties, and call methods in the SWF or the Flex application easily.
Through all of this, remember the Flash Player 8 security model. Be careful about loading SWF files and accessing information from other domains. For more information on the Flash Player 8 security model, see the Flash Player Developer Center.
File upload and download is a frequent request from many Flex developers. Now you can do it natively in the Flash Player instance without using the old method of calling the browser’s JavaScript to pass files around. This has a lot of benefits, primarily greater control over the user experience. Luckily, leveraging file upload and download in your Flex application is very simple. You can reuse the library of file transfer classes provided with this article so you don’t even have to write a line of Flash Player 8 code.
For this example, you learn how to use a small Flash Player 8 SWF file loaded into your Flex application. This Flash Player 8 content was created using the Flash 8 authoring tool. It has no user interface, so effectively it only provides you with a library of classes to use for file transfer; you continue to use Flex to build the entire interface and logic of your application.
There are actually two examples here. The first example is the simplest way to include file upload functionality in your Flex application and illustrates how you can include this feature in your applications. The second example elaborates on the simple file upload functionality and integrates the feature into a typical enterprise Flex architecture. It also gives you event logging, progress monitoring, and other useful features. Use the second example if you have a sophisticated, architected application.
This article provides several files for use with both examples:
In the simplest case, you can use the fileIO.swf file to give a user the functionality to select a file and then upload that file to the server. The ColdFusion file receives the file and stores it in the /upload directory. Use the SWF and ColdFusion files exactly as provided. The simpleFileTransfer.mxml file is as follows:
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
<mx:Button label="Browse" click="myLoader.content.browse('myFile')" id="browseButton" />
<mx:Button label="Upload" click="myLoader.content.upload('http://localhost:8300/upload.cfm')" id="uploadButton" />
<mx:Loader source="fileIO.swf" height="10" width="10" id="myLoader" />
</mx:Application>
This example simply shows how you can add a Loader component, which loads the SWF file, fileIO.swf. The two buttons execute a function in the loaded SWF file using the content parameter of the Loader component:
loader.content.function(parameters)
So, you can add basic file upload functionality to your Flex 1.5 application today using the fileIO.swf file provided in the Requirements section.
The second file transfer example provides a lot more functionality, and fits into best practices for Flex architecture. In this example, there are three MXML files in addition to the SWF and ColdFusion files. The three MXML files are as follows:
LibLoader.mxml: This is an MXML component that essentially wraps the Loader component with a few extra capabilities to manage the loaded event. You can use this component as a base class for your own projects.
FileIO.mxml: This is an MXML component that extends the LibLoader component with special browse, upload, download, and cancel methods. These methods are just wrappers that call the same functions in the fileIO.swf file. This file also adds a set of event listeners to catch status and error events.
main.mxml: This is a Flex application that includes the FileIO component and the rest of the Flex user interface to allow the user to select, upload, and download files; cancel them; and see the progress of the file transfer. In this file, the FileIO component is the Loader with the new capabilities added mainly in the FileIO.mxml file. Instead of calling functions in the loaded SWF file using loader.content, this file simply executes methods of the FileIO component.
This example illustrates that you can reuse a single SWF file with a library of Flash Player 8 functions for a wide range of needs in a wide range of applications. Use this SWF file for your file transfer implementations and choose a level of architectural sophistication that matches your application.
Once you are comfortable with this approach, you can use Flash 8 to create your own SWF files that leverage all sorts of great new features of Flash Player 8. You'll be able to leverage the ActionScript/JavaScript APIs and the expressiveness features to extend your Flex application using the same simple techniques.
Many thanks to Joan Tan, who is a quality assurance engineer on the Flex team. Joan contributed technical information and reviewed this article based on her background in working on Flex. Joan has also worked in quality assurance on Macromedia Central and Macromedia Flash.
Dirk Eismann is a software engineer at the Hannover, Germany–based software company Herrlich & Ramuschkat where he develops and implements web-based applications. Currently he focuses on Flex application architecture and the integration of Flex applications into Java and .NET environments. Dirk has been working with Flex since it hit the streets and now fluently speaks ActionScript 2.0 and MXML. He is also a Macromedia Certified Instructor for Flex and Flash, and an active contributor to the Flex community. He runs richinternet.blog, a blog dedicated to Flash-powered Internet applications.