Embedding text files in a Flex Application

One of my team member had a query on how to embed a text file in a Flex Application. We searched the help files but in vain.

The text cannot be retrieved if we use the following (Instead it will be better if I say, we didn’t find a way to retrieve it 🙂 ) –

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

Then I came across the way on how to do it (though I don’t remember the exact source).

I’m posting it here as it may be helpful to the other developers.

Here are the files –
TestingEmbeddedText.mxml
EmbeddedText.as
text.txt

Advertisements

10 thoughts on “Embedding text files in a Flex Application

  1. Hi,

    much easier is:

    [Embed(source=”text.txt”,mimeType=”application/octet-stream”)]
    private var TextClass : Class;

    var test:String = new TextClass;

  2. You can also embed AMF, a native serialized ActionScript Object. (which is scarcely documented on Adobe’s LiveDocs). Check out this example:

    public class StartupCommand
    {
    [Embed(source=”data/OnDemand1.amf”, mimeType=”application/octet-stream”)]
    private var embeddedData:Class;

    public function execute(inEvent:Event):void
    {
    var myBytes:ByteArray = new embeddedData()
    var myObject:Object = myBytes.readObject()

    var myEvent:LoadDataEvent = new LoadDataEvent(LoadDataEvent.LOAD)
    myEvent.data = myObject

    MateManager.instance.dispatcher.dispatchEvent(myEvent)
    }

    }

  3. works, but it is limited to just Ascii enconding (chars 0-127)

    I’ve tried several ways of changing the enconding without success. So, for encoding basic english text, this is allright, but when international characters are used, the only options I found it works is the Embed method

  4. See documentation for ByteArrayAsset. Here is excerpt:

    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);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s