Scenario:

  1. You have no write access to a deployment server, thus you have to save user-uploaded image to a Blob field in a database. Sad, but it happens.
  2. You need to read this photo entry in Flash, trying to stream it in and it shows nothing but gibberish.
  3. You might think that setting header("Content-type"); server side would work but IT DOESN’T.

Solution: Base 64 Encoding / Decoding

Note: I’m gonna use PHP server side scripting for this example

  1. Before saving the image data to the Blob field, base64_encode() the file stream. Say… you call this script saveimage.php
  2. Extract out the file from the server using normal query procedure, spit it out using echo. This would be your fetchimage.php
  3. Follow up with this AS3 script

Declare your variables
//variable declarations stuff
private var avatarString:String;
private var bitLoader:Loader;
private var containerImg:MovieClip;

Load the server response
var imgLoader:URLLoader = new URLLoader();
imgLoader.dataFormat = URLLoaderDataFormat.TEXT;
imgLoader.load(new URLRequest("https://www.domain.com/fetchimage.php?id=12345"));
imgLoader.addEventListener(Event.COMPLETE, onDataLoad);

Handle data load
function onDataLoad(evt:Event){
avatarString = evt.target.data.toString();
var decodedString:String = Base64.Decode(avatarString);
var imageBytes:ByteArray = new ByteArray();
for(var i:Number=0; i < decodedString.length ; i++){
imageBytes.writeByte(decodedString.charCodeAt(i));
}

And display it…
bitLoader = new Loader();
bitLoader.loadBytes(imageBytes);
containerImg.addChildAt(bitLoader,0);



Comments

  1. 1
    the jaffry
    October 22nd, 2008 at 3:42 am

    all i can say… WAH LAU EH!

    haha… good tip though.

Leave a Comment

blank