Embedding text files in a Flex Application

Embedding text files in a Flex Application

To embed an arbitrary file, you declare a variable of type Class, and put [Embed] metadata on it, using the MIME type application/octet-stream. For example, you embed a text file like this:

[Bindable]
[Embed(source="Story.txt", mimeType="application/octet-stream")]
private var storyClass:Class;

The compiler autogenerates a subclass of the ByteArrayAsset class and sets your variable to be a reference to this autogenerated class. You can then use this class reference to create instances of the ByteArrayAsset using the new operator, and you can extract information from the byte array using methods of the ByteArray class:

var storyByteArray:ByteArrayAsset = ByteArrayAsset(new storyClass());
var story:String = storyByteArray.readUTFBytes(storyByteArray.length);

你可能感兴趣的:(Embedding text files in a Flex Application)