我需要将一个字节的数据数组(它是一个图像源)与一堆其他变量一起发送到服务。如果我使用以下内容发送字节数组
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 );
并在 php
$fp = fopen( 'myImage.jpg', 'wb' );
fwrite( $fp, $GLOBALS[ 'HTTP_RAW_POST_DATA' ] );
fclose( $fp );
那么这可以很好地保存图像。但是我需要向下发送额外的变量,所以我尝试使用以下内容。
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 );
然后在php中
$byteArray= $_REQUEST["imageArray"];
$fp = fopen( 'myImage.jpg', 'wb' );
fwrite( $fp, $byteArray );
fclose( $fp );
但这行不通。保存文件的文件大小不同,后者不保存为图像。我错过了什么。是否工作内容类型是 application/octet-stream 而不起作用的内容类型是 application/x-www-form-urlencoded?