如何将 ByteArray(来自 Flash)和一些表单数据发送到 php?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/597947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I send a ByteArray (from Flash) and some form data to php?
提问by
I have a sketch pad made in Flash AS3 like this one here: http://henryjones.us/articles/using-the-as3-jpeg-encoder
我有一个用 Flash AS3 制作的画板,就像这样:http: //henryjones.us/articles/using-the-as3-jpeg-encoder
I want to send the jpg to the server and some data from a html form field with php. Is there a way to force the Flash movie to deliver the image file when one presses the submit button?
我想将 jpg 发送到服务器,并使用 php 将 html 表单字段中的一些数据发送到服务器。有没有办法在按下提交按钮时强制 Flash 电影传送图像文件?
采纳答案by quoo
it's a little tricky: You can do it one of two ways:
这有点棘手:您可以通过以下两种方式之一进行操作:
Arthem posted this in another thread:
Arthem 在另一个帖子中发布了这个:
This was of great help to me: http://www.quietless.com/kitchen/upload-bitmapdata-snapshot-to-server-in-as3/
You need to modify the URLRequestWrapper to insert field names and file names where needed. Here's what I've done:
bytes = 'Content-Disposition: form-data; name="' + $fieldName + '"; filename="';
It does the most formatting of headers so the server could understand it as a file upload.
By the way, if you have a BitmapData you might need to encode it to JPEG or PNG first.
这对我有很大帮助:http: //www.quietless.com/kitchen/upload-bitmapdata-snapshot-to-server-in-as3/
您需要修改 URLRequestWrapper 以在需要的地方插入字段名和文件名。这是我所做的:
bytes = '内容处理:表单数据;名称="' + $fieldName + '"; 文件名="';
它对标题进行了最多的格式化,因此服务器可以将其理解为文件上传。
顺便说一下,如果您有 BitmapData,您可能需要先将其编码为 JPEG 或 PNG。
And I usually use this solution:
我通常使用这个解决方案:
I haven't used the imageshack API, but you may want to try using adobe's JPGEncoder class - here's a quick example that passes a username and the JPG's byte array, it's really quite simple.
我没有使用过imageshack API,但您可能想尝试使用adobe 的JPGEncoder 类——这是一个传递用户名和JPG 字节数组的快速示例,它真的很简单。
private function savePicToServer(bmpData:BitmapData):void
{
var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(bmpData);
var loader:URLLoader = new URLLoader();
configureListeners(loader);
var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest(ModelLocator.BASE_URL + ModelLocator.UPLOAD_URL + "?user=" + ModelLocator.getInstance().username);
request.requestHeaders.push(header);
request.method = URLRequestMethod.POST;
request.data = jpgStream;
loader.load(request);
}
Note that the variables are passed as part of the query string. You can't use URLVariables, as several people have suggested, as the URLVariables would be stored in the request's data property, which we are already using to pass the byteArray.
请注意,变量作为查询字符串的一部分传递。您不能使用 URLVariables,正如一些人所建议的那样,因为 URLVariables 将存储在请求的 data 属性中,我们已经使用它来传递 byteArray。
回答by Icono
I wanted to send an encrypted authorization code form Flash to PHP along with a JPG image. It was not suitable to post this in the get variables and so I got around it this way. Most of the code is the actual gathering of a stage snapshot, but only a specific region of it. The part that is relevant to here is the use of writeMultiByte to tack on the encrypted password to the end of the jpb image info. I then send the length of this password using the get variables url string so that PHP know show much to chop off the end.
我想将加密的授权代码形式 Flash 与 JPG 图像一起发送到 PHP。不适合将其发布在 get 变量中,因此我以这种方式解决了这个问题。大多数代码是阶段快照的实际收集,但只是它的特定区域。与此处相关的部分是使用 writeMultiByte 将加密密码附加到 jpb 图像信息的末尾。然后,我使用 get variables url 字符串发送此密码的长度,以便 PHP 知道要截断结尾的内容。
var stage_snapshot:BitmapData = new BitmapData(600, 120);
var myRectangle:Rectangle = new Rectangle(0, 0, 600, 120);
var myMatrix:Matrix = new Matrix();
var translateMatrix:Matrix = new Matrix();
translateMatrix.translate(-101, -33);
myMatrix.concat(translateMatrix);
stage_snapshot.draw(stage,myMatrix,null,null,myRectangle);
trace ("creating JPGEncoder");
// Setup the JPGEncoder, run the algorithm on the BitmapData, and retrieve the ByteArray
var encoded_jpg:JPGEncoder = new JPGEncoder(100);
var jpg_binary:ByteArray = new ByteArray();
jpg_binary = encoded_jpg.encode(stage_snapshot);
var pw = cryptoSalt.encrypt("mysecretpassword");
**var pwLength = pw.length;
jpg_binary.writeMultiByte(pw,"us-ascii");**
trace ("stage snapshot taken");
var header:URLRequestHeader = new URLRequestHeader ("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest("savejpg.php?length1=" + pwLength);
request.requestHeaders.push(header);
request.method = URLRequestMethod.POST;
request.data = jpg_binary;
var loader:URLLoader = new URLLoader();
trace ("sending pic to php");
loader.load(request);
loader.addEventListener(Event.COMPLETE,finishSave);
The receiving PHP:
接收PHP:
$pwlength = $_GET['length1'];
$jpg = $GLOBALS["HTTP_RAW_POST_DATA"];
$pw = substr($jpg, - $pwlength);
$jpg = substr($jpg,0,strlen($jpg) - $pwlength);
$filename = 'uploads/test.jpg';
file_put_contents($filename, $jpg);
file_put_contents("uploads/pw.txt",$pw);
回答by JH Studio
You can use AMFPHP or Zend AMF, I just finished a project like this, my product allow a user to change a product color scheme and then output the ByteArray data and other AS3 object directly to PHP, and insert into MySQL, you can see my projectand AMFPHP
你可以使用AMFPHP或者Zend AMF,我刚刚完成了一个这样的项目,我的产品允许用户更改产品配色方案,然后将ByteArray数据和其他AS3对象直接输出到PHP,并插入到MySQL中,你可以看到我的项目和AMFPHP
all PNG transparent images are generated by Flash, and send to AMFPHP, PHP then generate the PNG file on the server.
所有PNG透明图片均由Flash生成,发送到AMFPHP,PHP再在服务器上生成PNG文件。
回答by dirkgently
You can use a HTTPServiceto send data. Here'sa tutorial sort introduction.
您可以使用HTTPService发送数据。这是一个教程排序介绍。
回答by Carlo
If you're not using flex, try URLLoader && URLrequest, to pass your data as a POST variable.
如果您不使用 flex,请尝试使用 URLLoader && URLrequest,将您的数据作为 POST 变量传递。

