开发者

Save multiple images from Flash to server using JPGEncoder?

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.d开发者_运维百科ata);
}

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


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜