需要使用HTTPService而不是URLRequest来发送数据。内容类型弄乱了数据?

发布于 2024-11-26 20:18:07 字数 1335 浏览 4 评论 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 ?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

菩提树下叶撕阳。 2024-12-03 20:18:07

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

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 代码完成了以下操作。

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 图像。不理想,但它有效,我仍然想要一个不涉及对字节数组进行编码的解决方案。因此,尽管我已经找到了这个解决方法,但请随意回答。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文