How can I send a ByteArray (from Flash) and some form data to php?

Solution 1:

it's a little tricky: You can do it one of two ways:

Arthem posted this in another thread:

This was of great help to me: http://www.quietless.com/kitchen/upload-bitmapdata-snapshot-to-server-in-as3/

You need to modify the URLRequestWrapper to insert field names and file names where needed. Here's what I've done:

bytes = 'Content-Disposition: form-data; name="' + $fieldName + '"; filename="';

It does the most formatting of headers so the server could understand it as a file upload.

By the way, if you have a BitmapData you might need to encode it to JPEG or PNG first.

And I usually use this solution:

I haven't used the imageshack API, but you may want to try using adobe's JPGEncoder class - here's a quick example that passes a username and the JPG's byte array, it's really quite simple.

private function savePicToServer(bmpData:BitmapData):void
{
    var jpgEncoder:JPGEncoder = new JPGEncoder(85);
    var jpgStream:ByteArray = jpgEncoder.encode(bmpData);

    var loader:URLLoader = new URLLoader();
        configureListeners(loader);

    var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
    var request:URLRequest = new URLRequest(ModelLocator.BASE_URL + ModelLocator.UPLOAD_URL + "?user=" + ModelLocator.getInstance().username);
    request.requestHeaders.push(header);
    request.method = URLRequestMethod.POST;
    request.data = jpgStream;
    loader.load(request);
}

Note that the variables are passed as part of the query string. You can't use URLVariables, as several people have suggested, as the URLVariables would be stored in the request's data property, which we are already using to pass the byteArray.

Solution 2:

I wanted to send an encrypted authorization code form Flash to PHP along with a JPG image. It was not suitable to post this in the get variables and so I got around it this way. Most of the code is the actual gathering of a stage snapshot, but only a specific region of it. The part that is relevant to here is the use of writeMultiByte to tack on the encrypted password to the end of the jpb image info. I then send the length of this password using the get variables url string so that PHP know show much to chop off the end.

var stage_snapshot:BitmapData = new BitmapData(600, 120);
var myRectangle:Rectangle = new Rectangle(0, 0, 600, 120);
var myMatrix:Matrix = new Matrix();
var translateMatrix:Matrix = new Matrix();
translateMatrix.translate(-101, -33);
myMatrix.concat(translateMatrix);
stage_snapshot.draw(stage,myMatrix,null,null,myRectangle);
trace ("creating JPGEncoder");
// Setup the JPGEncoder, run the algorithm on the BitmapData, and retrieve the ByteArray
var encoded_jpg:JPGEncoder = new JPGEncoder(100);
var jpg_binary:ByteArray = new ByteArray();
jpg_binary = encoded_jpg.encode(stage_snapshot);
var pw = cryptoSalt.encrypt("mysecretpassword");
**var pwLength = pw.length;
jpg_binary.writeMultiByte(pw,"us-ascii");**
trace ("stage snapshot taken");
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest("savejpg.php?length1=" + pwLength);
request.requestHeaders.push(header);
request.method = URLRequestMethod.POST;
request.data = jpg_binary;
var loader:URLLoader = new URLLoader();
trace ("sending pic to php");
loader.load(request);
loader.addEventListener(Event.COMPLETE,finishSave);

The receiving PHP:

$pwlength = $_GET['length1'];
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
$pw = substr($jpg, - $pwlength);
$jpg = substr($jpg,0,strlen($jpg) - $pwlength);
$filename = 'uploads/test.jpg';
file_put_contents($filename, $jpg);
file_put_contents("uploads/pw.txt",$pw);