使用JPGEncoder将多个图像从Flash保存到服务器?

问题描述:

I'm trying to encode and upload two (or more) images from Flash using JPGEncoder. I can upload a single image with no problem, but have never needed to upload two or more encoded images. This is the code I'm using to encode and upload a single image.

var bmd:BitmapData = new BitmapData(img.width,img.height,true,0);
bmd.draw(img);

var ba:ByteArray = new JPGEncoder(90).encode(bmd);

var vars:Object = new Object();
vars.path = 'uploads/';

var urlRequest : URLRequest = new URLRequest();
urlRequest.url = 'http://example.com/image.php';
urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
urlRequest.method = URLRequestMethod.POST;
urlRequest.data = UploadPostHelper.getPostData('image.jpg',ba,vars);
urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache'));

var urlLoader : URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.addEventListener(Event.COMPLETE, loadComplete);
urlLoader.load(urlRequest);

function loadComplete(e:Event):void {
    trace(e.target.data);
}

Also, here is the UploadPostHelper helper class that I'm using. Has anyone done this? Can you point me in the right direction?

我正在尝试使用JPGEncoder对来自Flash的两个(或更多)图像进行编码和上传。 我可以毫无问题地上传单个图像,但从不需要上传两个或更多编码图像。 这是我用来编码和上传单个图像的代码。 p>

  var bmd:BitmapData = new BitmapData(img.width,img.height,true,0)  ; 
bmd.draw(img); 
 
var ba:ByteArray = new JPGEncoder(90).encode(bmd); 
 
var vars:Object = new Object(); 
vars.path ='uploads /'  ; 
 
var urlRequest:URLRequest = new URLRequest(); 
urlRequest.url ='http://example.com/image.php';
urlRequest.contentType ='multipart / form-data;  boundary ='+ UploadPostHelper.getBoundary(); 
urlRequest.method = URLRequestMethod.POST; 
urlRequest.data = UploadPostHelper.getPostData('image.jpg',ba,vars); 
urlRequest.requestHeaders.push(new URLRequestHeader('  Cache-Control','no-cache')); 
 
var urlLoader:URLLoader = new URLLoader(); 
urlLoader.dataFormat = URLLoaderDataFormat.BINARY; 
urlLoader.addEventListener(Event.COMPLETE,loadComplete); 
urlLoader。  load(urlRequest); 
 
function loadComplete(e:Event):void {
 trace(e.target.data); 
} 
  code>  pre> 
 
 

另外 ,这是我正在使用的UploadPostHelper助手类。 有没有人这样做过? 你能指出我正确的方向吗? p> div>

If you place all of your JPEG encoding and uploading into a function, you can call that function from loadComplete(e:Event). An example name for the function could be urlEncodeUpload() Call the function once from your code and when it signals the loadComplete, it can call itself again (somewhat like an iterator, but using event driven style).

You'd need to check first whether you have any images left to encode and upload. If you have all of your images in an Array of their own, you could pop() the next bitmap until it's empty, and pass that image to the urlEncodeUpload() function.

function urlEncodeUpload(img:*):void {
        var bmd:BitmapData = new BitmapData(img.width,img.height,true,0);
        bmd.draw(img);

        var ba:ByteArray = new JPGEncoder(90).encode(bmd);

        var vars:Object = new Object();
        vars.path = 'uploads/';

        var urlRequest : URLRequest = new URLRequest();
        urlRequest.url = 'http://example.com/image.php';
        urlRequest.contentType = 'multipart/form-data; boundary=' + UploadPostHelper.getBoundary();
        urlRequest.method = URLRequestMethod.POST;
        urlRequest.data = UploadPostHelper.getPostData('image.jpg',ba,vars);
        urlRequest.requestHeaders.push(new URLRequestHeader('Cache-Control', 'no-cache'));

        var urlLoader : URLLoader = new URLLoader();
        urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
        urlLoader.addEventListener(Event.COMPLETE, loadComplete);
        urlLoader.load(urlRequest);
    }

function loadComplete(e:Event):void {
    if (imgArray.length > 0) urlEncodeUpload(imgArray.pop());
}

Hope that helps..