10

Base64 Encoding and Decoding in JavaScript in Adobe AIR

 3 years ago
source link: http://www.mikechambers.com/blog/2008/06/12/base64-encoding-and-decoding-in-javascript-in-adobe-air/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Base64 Encoding and Decoding in JavaScript in Adobe AIR

Thursday, June 12, 2008

I have been working on a JavaScript AIR app for the past week or so while on the on AIR Tour in Europe, and ran into the need to Base64 encode some data. I knew that there were some JavaScript libraries that did this, and I could also create a SWF library that used the Flex Base64Encoder class. However, I didnt like either of these solutions as I was concerned about performance with using a JavaScript implementation, and didnt want to hassle with creating a SWF library to link into JavaScript.

Well, after some searching on Google, I discovered that webkit has native Base64 encoding and decoding functions, respectively named btoa and atob.

Here is how you can use them from JavaScript:

var s = "foo";
var encoded = btoa(s);
var decoded = atob(encoded);
window.runtime.trace(s);
window.runtime.trace(encoded);
window.runtime.trace(decoded);

This outputs:

foo
Zm9v
foo

Among other things, this can come in handy if you need to create custom HTTP authentication headers.

You can find more information on the apis here.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK