Is it possible to convert an selected image into base64 encoded string?
Would be nice and easy solution for image uploader. :)
Thanks ;)
If you're wanting to encode the byteArray of a loaded image, you could use the Base64Encoder class from mx.utils Base64Encoder.
Something like:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
loader.load(new URLRequest("img.jpg"));
function loadComplete(e:Event):void {
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loadComplete);
var bmd:BitmapData = Bitmap(e.target.content).bitmapData;
var ba:ByteArray = bmd.getPixels(new Rectangle(0,0,bmd.width,bmd.height));
var b64:Base64Encoder = new Base64Encoder();
b64.encodeBytes(ba);
trace(b64.toString());
}
I had to track down the class here.
Also, there's another Base64 class that I found but haven't tested here...but looks like it works similarly.
Hope that helps.
You can save an image as a Base64-string, but I wouldn't recommend it. I have tried doing this and it slows down your application a lot.
If you still want to do this, you should download the Base64-class at this link: http://garry-lachman.com/2010/04/21/base64-encoding-class-in-actionscript-3/
If you then get the bitmapData from your image, you can call for the .getPixels()-method, which returns a bytearray. This bytearray can be converted to a Base64-string using the class in the link.
If you want to load images from a Base64-string, you can create a Loader-object and use the loadBytes()-method to load in the byteArray you get by decoding your Base64-string.
Hope this helps :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With