如何将 BitmapData 上传到服务器 (ActionScript 3)?
我有位图数据。我想使用 URLLoader 将其上传到服务器。我尝试了很多方法,但没有结果。 这是我当前在 ActionScript 3 中的代码:
import flash.net.URLLoader;
import flash.net.URLRequest;
import mx.graphics.codec.JPEGEncoder;
...
var jpg:JPEGEncoder = new JPEGEncoder();
var myBytes:ByteArray = jpg.encode(bitmapData);
var uploader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("uploadFile.php");
request.contentType = "application/octet-stream";
request.data = myBytes;
uploader.load(request);
我获取一个对象 bitmapData 并将其编码为 jpg。之后,我尝试将 jpg 字节发送到服务器上的文件“uploadFile.php”。但既没有错误也没有积极的结果。 我将不胜感激任何建议/意见。
I have bitmapData. I want to upload it to a server using URLLoader. I tried many ways, but with no result.
This is my current code in ActionScript 3:
import flash.net.URLLoader;
import flash.net.URLRequest;
import mx.graphics.codec.JPEGEncoder;
...
var jpg:JPEGEncoder = new JPEGEncoder();
var myBytes:ByteArray = jpg.encode(bitmapData);
var uploader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("uploadFile.php");
request.contentType = "application/octet-stream";
request.data = myBytes;
uploader.load(request);
I take an object bitmapData and encode it to jpg. After that I try to send jpg-bytes to the file "uploadFile.php" on the server. But there are neither errors nor positive result.
I will be grateful for any suggestions/advices.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我之前使用过的 AS3 代码 - 这会在请求正文中发出带有二进制数据的 POST 请求:
我注意到您没有将 .dataFormat 设置为 BINARY 或将 .method 设置为 POST。
请注意,这可能是您遇到的安全问题。我见过这样的代码必须执行以响应用户操作(例如单击按钮)的情况。
您还应该检查服务器日志以查看请求是否发送到服务器。请注意,如果没有完全限定的 url(以 http:// 开头),它会假定服务器从与 SWF 文件相同的位置提供 PHP 文件。
This is the AS3 code I've used for this before - this makes a POST request with binary data in the request body:
I notice that you're not setting .dataFormat to BINARY or .method to POST.
Note that it could be a security issue you're running into. I've seen cases where code like this must execute in response to a user action (like a button click).
You should also check your server logs to see whether the request is making it to the server. Note that without a fully-qualified url (starts with http://), it assumes the server serves your PHP file from the same location as your SWF file.
您对图像的编码会破坏它,因为 jpg 数据包含非 ASCII 字符。
将您的 contentType 类型更改为“image/jpeg”应该可以修复它,但是此语句未经测试。
当我将 JPG 发送到服务器时,我总是先对其进行 Base64 编码,然后在服务器端对其进行解码。
Your encoding of the image will break it because jpg data contains non-ascii characters.
Changing your contentType type to "image/jpeg" should fix it however this statement is untested.
When I send JPG to the server I always base64 encode it first and then decode it on the server side.