I need to send a byte 开发者_JAVA技巧array of data (its an image source) along with a bunch of other vars to a service. If I send the byte array using something like the following
var request:URLRequest = new URLRequest ( 'http://www.mydomain.com/upload.php' );
var loader: URLLoader = new URLLoader();
request.contentType = 'application/octet-stream';
request.method = URLRequestMethod.POST;
request.data = byteArrayOfImage;
loader.load( request );
and in the php
$fp = fopen( 'myImage.jpg', 'wb' );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
then this saves the image fine. But I need to send extra vars down so I'm trying to use the following.
var service : HTTPService = new HTTPService();
service.method = "POST";
service.contentType = 'application/x-www-form-urlencoded';
service.url = 'http://www.mydomain.com/upload.php';
var variables : URLVariables = new URLVariables();
variables.imageArray = myImageByteArray;
variables.variable2 = "some text string";
variables.variable3 = "some more text";
service.send( variables );
Then in the php
$byteArray= $_REQUEST["imageArray"];
$fp = fopen( 'myImage.jpg', 'wb' );
fwrite( $fp, $byteArray );
fclose( $fp );
But this isn't working. The file sizes of the saved files are different and the later doesn't save it as an image. What am I missing. Is it that the working content type is application/octet-stream and the content type of the one that doesn't work is application/x-www-form-urlencoded?
I've found a similar question which gives a workaround. Not ideal, but it works.
http://www.google.es/search?sourceid=chrome&ie=UTF-8&q=as3+base64+encoder
http://code.google.com/p/jpauclair-blog/source/browse/trunk/Experiment/Base64/src/Base64.as
So what I've done is the following using the Base64 code.
var encodedString : String = Base64.encode( imageByteArray );
var service : HTTPService = new HTTPService();
service.method = "POST";
service.contentType = 'application/x-www-form-urlencoded';
service.url = 'http://www.mydomain.com/upload.php';
var variables : URLVariables = new URLVariables();
variables.imageArray = encodedString;
variables.variable2 = "some text string";
variables.variable3 = "some more text";
service.send( variables );
then in the php side
$byteArray= $_REQUEST["imageArray"];
$byteArray= base64_decode($byteArray);
$fp = fopen( 'myImage.jpg', 'wb' );
fwrite( $fp, $byteArray);
fclose( $fp );
This saves a valid jpg image. Not ideal but it works, I'd still like a solution that doesn't involve encoding the byte array. So feel free to answer even though I've found this workaround.
精彩评论