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?
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 beurlEncodeUpload()Call the function once from your code and when it signals theloadComplete, 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 theurlEncodeUpload()function.Hope that helps..