0

我需要将一个字节的数据数组(它是一个图像源)与一堆其他变量一起发送到服务。如果我使用以下内容发送字节数组

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?

4

1 回答 1

0

我发现了一个类似的问题,它提供了一种解决方法。不理想,但它有效。

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

所以我所做的是以下使用 Base64 代码。

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 );

然后在php端

$byteArray= $_REQUEST["imageArray"];
$byteArray= base64_decode($byteArray);
$fp = fopen( 'myImage.jpg', 'wb' );
fwrite( $fp, $byteArray);
fclose( $fp );

这会保存一个有效的 jpg 图像。不理想但它有效,我仍然想要一个不涉及编码字节数组的解决方案。因此,即使我找到了这种解决方法,也可以随意回答。

于 2011-07-28T10:24:38.650 回答