30 August 2007

Base64 Encoding in Flash (using MIME)

Written by Richard Leggett ( Contact the author of this post )
Published on August 30th, 2007 @ 09:31:49 pm, using 261 words, 2151 views
Categories: Flash, Flex, AIR/Apollo

Base64 encoding is a very useful technique that allows for the sending of binary data, such as an image file, over the wire using plain-text. This means it can be embedded in XML (plain, SOAP or XML-RPC), it also means the binary data can be stored very easily in a database. It works by reading in data in packs of 6 bits, and turning each one of those into a character in a simple 64 character alphabet. This input data can be text, but it can also be binary data stored in a ByteArray; perhaps the contents of a file, or an MP3 your Flash application has constructed in memory.

Here's a quick tip that might save you some time and bother. If you are doing any Base64 encoding in ActionScript (for example when sending email attachments or uploading images to a blog over XML-RPC) make sure you split your string up into lines of 76 characters (as is defined in the MIME spec), and add an extra line break at the end. Some Base64 decoders do not require this, but in a lot of common cases for Base64 it is required.

There are already several implementations out there of Base64 encoders/decoders for AS2 and AS3, I was under the impression there was an mx.utils.Base64Encoder class in the Flex but I was not able to find it. This tool is very handy for comparing your results. It also gives the option to upload and convert a file, and specify how many characters to use per line (76 being the common case).

About

Richard Leggett is a Senior Creative Developer at AKQA, London. He is co-author of Foundation Flash Applications for Mobile Devices (Friend of ED), an Adobe Community Expert and speaker at industry conferences and user groups.


August 2007
Mon Tue Wed Thu Fri Sat Sun
 << < Current> >>
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Categories

Miscellany

XML Feeds

Information

Contact the admin  /   b2evo template by Two18 Media