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

  • Shunjie

    Posted: August 31, 2007


    Heres a tip on how to convert them in AS3 ;)

    http://expertria.com/index.php/archives/30


  • Rich

    Posted: August 31, 2007


    Hi Shunjie,

    Great tip thanks indeed, I should post the code I'm using to read in from a file soon.

    I wanted to ask you, where have you found the mx.utils.Base64Encoder class? It is not available in the Moxie SDK, perhaps in 2.0.1?.


  • Shunjie

    Posted: September 5, 2007


    Hi Richard,

    You can find it in the Flex 3 SDK (Movix) at

    /frameworks/projects/framework/src/mx/utils

    I think its not in the docs but you can use it ;)


  • Rich

    Posted: September 5, 2007


    Thanks for the reply Shunjie, it seems I have that class missing still. Never mind, the alternative works very well for now.


  • JesterXL

    Posted: September 25, 2007


    For URL encoding, I've found Base64 isn't safe if you can't use CDATA (*ahem* *cough* Google's XML parser *cough*), so, throw it at a encodeURIComponent call, Base64 it, and then encodeURIComponent again for good measure. Always works!


  • Rich

    Posted: September 25, 2007


    Nice tip, cheers Jesse! Unfortunately some specs prevent you from using CDATA (like XMLRPC), but otherwise definitely the better option to CDATA.


  • anoop

    Posted: May 12, 2008


    An error occur during d usage of Base64...the code i used is

    var username:String = "username";
    var password:String = "password";
    var encodedCredentials:String = Base64.encode(username + ":" + password);
    var headerArray = new Array(
    new URLRequestHeader("Authentication",
    "Basic " + encodedCredentials));
    request.headers = headerArray;
    request.request = { status: tweet_txt.text };
    request.send();

    ;;;;;here tell d error access of undefined property Base64...can u help me


  • Vikky

    Posted: August 30, 2009


    Hi,

    Does any one has an AS file for Flash AS2?
    Which will convert the given file to a base64 format? Or where can I find it?

    Thanks in advance :)