需要使用HTTPService而不是URLRequest来发送数据。内容类型弄乱了数据?
我需要将一个字节数据数组(它的图像源)以及一堆其他变量发送到服务。 使用类似以下内容发送字节数组
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 ?
I need to send a byte 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现了一个类似的问题,它提供了解决方法。不理想,但它有效。
http://www.google.es /search?sourceid=chrome&ie=UTF-8&q=as3+base64+编码器
http://code.google.com /p/jpauclair-blog/source/browse/trunk/Experiment/Base64/src/Base64.as
所以我使用 Base64 代码完成了以下操作。
然后在 php 端
保存一个有效的 jpg 图像。不理想,但它有效,我仍然想要一个不涉及对字节数组进行编码的解决方案。因此,尽管我已经找到了这个解决方法,但请随意回答。
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.
then in the php side
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.