Send And Upload Binary Bytes or Image from Flash with ActionScript 2.0

Salam & Hi there, I had a project the last couple of month that require sending Image from Flash to Web and since I did not fully move yet to AS3.0 I had to use the AS2.0 to send Binary data to server, I searched the internet and did not find much useful information for AS2.0 mianly, however I came up with a simple idea to do that job and it’s seems working.

Uploading and sending Image from Flash to Web
 

Encode Bytes to String.

Yes, weather you have a BitmapData object or whatever you want to transfer from flash to your webserver, try to encoded it to String, that will help a lot, the string size maybe bigger than the original chunk of data ( actually almost the double or so ) but it works pretty well .
Use the .toString(16) to encode.
We know how to use the .toString() Shared Member Function, if you used a base-numeric system as a parameter for encoding it should work, example .

var iNum = 10;
trace(iNum1.toString(2)); //outputs “1010”
trace (iNum1.toString(8)); //outputs “12”
trace (iNum1.toString(16)); //outputs “A”

And as you see we will use the hexadecimal (16) because it gives a smaller length reprehensive string, there are other developer that works with Base64 encoding but I did not use here .
Now all you have to do is to convert your Bytes into reprehensive String.

var strBytes = new String();
strBytes+=myByte.toString(16);

Your bytes-string should looks like this on the end
trace (strBytes); // output "AFC5FECD3DF8C6D7FF" …

How to send the bytes data string to server .

Either you’re using PHP, ASP or whatever language; as long as you’re using a string you can send it to server and through Flash, you can use the XML Class Object but as for my test the best way to do is to use LoadVars Class Object because of the POST method which allow us to send large length of bytes string  ( file size ).
Check the example below,  we are using a lvDataSender instance which use to send our strBytes ( bytes-string ).

var lvDataSender:LoadVars = new LoadVars();
lvDataSender.onLoad = function(success:Boolean) {
        if (success) { // if the data sent successfully
                trace("sent :)!") ;
            } else {
                trace("ops :S"); // if failed
            };
     };// on load
lvDataSender.bytesString= strBytes; // bytesString is your variable-name which holding data on server side.
lvDataSender.sendAndLoad("http://mysite.com/page.php" , this,  "POST"); // execute

Now the hardest example …

How to Encode & Send image or BitmapData from Flash to WebServer

As we said we need to convert Post Views: 135