explorers' club

explorations in dev, science, sci-fi, games, and other fun stuff!

AIR: ByteArray & FileStream


[note to self]

In some scenarios where an attachment is being sent along as a multipart form it is necessary to send the byteArray representation of the attachment.  One way of accessing this is to use the flash.filesystem.FileStream class.  The problem w/ accessing the raw byteArray data is that the FileStream API doesn’t have any methods that return a byteArray.  Maybe I am missing the point but I would have assumed that if you wanted to access the byteArray data from a fileStream, there would be an API that would look something like:

var ba:ByteArray = fileStream.getByteArray();

Unfortunately this is not the case.  Instead this is how you do it.  It’s not the most intuitive but it works nonetheless:

var file:File = File(evt.target); //this is being triggered from a FileReference Event.SELECT
file.removeEventListener(Event.SELECT, fileSelected);
attachmentName.text = file.nativePath;

var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var ba:ByteArray = new ByteArray();
stream.readBytes(ba); //this is the equivalent to stream.getByteArray():ByteArray

4 thoughts on “AIR: ByteArray & FileStream

  1. Good blog, userful, thanks.

  2. Thanks!
    It’s really what I need.
    I going to use this function on Android.

  3. I do want to load Sound data in ByteArray. Is it possible, if so how?

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s