02
Aug
08

Embedding text file into flex

There are some situations in which we want to embedd the files either it may text file or any other kind of files into the flex. We know how to embedding the files likeĀ  JPEG, GIF, PNG, SWF, Sound, Fonts etc.

Flex also have an option to embed any kind of file at compile time. The trick is with the ‘mimeType‘ option of the [Embedd] tag. while embedding other kind of files like text (or any) we needĀ  to specify ‘application/octet-stream‘ for mimeType option. Here is the sample;

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

Once you embedded the file like the above, the flex considers the file as ByteArrayAsset, and reference
that ByteArrayAsset to the specified variable, in our case, myFileClass variable, so we can access the
embedded file in our application, like below;

var MyFileByteArray:ByteArrayAsset = ByteArrayAsset(new myFileClass());
var story:String = MyFileByteArray.readUTFBytes(MyFileByteArray.length);

The mimeType=”application/octet-stream” option makes all the difference here. It converts the specified
source to Bytes of array.


2 Responses to “Embedding text file into flex”


  1. November 25, 2009 at 6:09 pm

    Many of folks talk about this subject but you said really true words.


Leave a Reply