Awhile back one of the guys on my team discovered an interesting way to embed files in ActionScript using the “application/octet-stream” mimeType with the Embed metadata tag.
When using the Embed tag it is not common to explicitly assign a specific mimeType to an asset. Because of this Flex uses heuristics to determine the appropriate mimeType based on the extension of the embedded asset (i.e. file).
The Adobe Flex documentation states:
“You can use the [Embed] metadata tag to import JPEG, GIF, PNG, SVG, SWF, TTF, and MP3 files.”
However when specifying application/octet-stream (which is essentially an arbitrary byte stream ) as the assets mimeType it is also possible to embed a file of any type. If the file type is not supported natively, such as XML, developers can write custom parsers which can read the file utilizing the ByteArray (Flex 2) or ByteArrayAsset (Flex 3).
Below I have provided a basic example which demonstrates how the application/octet-stream mimeType can be utilized to embed a file, in this case an external xml document which serves as a config file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package example { import mx.core.ByteArrayAsset; public final class Config { [Embed("config.xml", mimeType="application/octet-stream")] private static const Config:Class; public static function getConfig() : XML { var ba:ByteArrayAsset = ByteArrayAsset( new Config()) ; var xml:XML = new XML( ba.readUTFBytes( ba.length ) ); return xml; } } } |
In the above example an XML document is embedded as a Class object from which the contents of the file are read as a string via ByteArrayAsset and converted to an XML object.
Based on this example it would also be possible to create arbitrary custom types in conjunction with custom parsers to allow additional support for numerous other file types in Flex as well.
I’ve successfully used this technique to embed JavaScript code in a Flex application. The JavaScript is then deployed to the browser via ExternalInterface. It’s useful when your Flex/Flash application depends on some JavaScript to do what it does because it’s one less external dependency.
Here’s my description of what I did:
http://blog.iconara.net/2007/12/28/deep-linking-in-flex-using-swfaddress-and-browsermanager/
thats a really kick ass technique… thanks for sharing…