How to upload BitmapData to a server (ActionScript 3)?

天涯浪子 提交于 2019-12-06 13:18:15

问题


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.


回答1:


This is the AS3 code I've used for this before - this makes a POST request with binary data in the request body:

var url_request:URLRequest = new URLRequest();
url_request.url = "http://server.com/upload.php";
url_request.contentType = "binary/octet-stream";
url_request.method = URLRequestMethod.POST;
url_request.data = myByteArray;
url_request.requestHeaders.push(
 new URLRequestHeader('Cache-Control', 'no-cache'));

var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
// attach complete/error listeners
loader.load(url_request);

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.




回答2:


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.

import mx.utils.Base64Encoder;

var b64:Base64Encoder = new Base64Encoder()
b64.encodeBytes( myBytes )
var encodedb64.toString();

request.data = b64.toString();


来源:https://stackoverflow.com/questions/8854952/how-to-upload-bitmapdata-to-a-server-actionscript-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!