I have a Flex application that allows users to build simple slides, a la Powerpoint, by entering text, uploading images and selecting from a clip art library. At the end of the process, I'm writing the contents of the canvas object to a ByteArray and would like to upload it to an ASP.net webserver. I have scripts on the server that accept user uploads via FileReference, for uploading backgrounds and images, but am running into a wall trying to upload the final image. What I need is something like what happens with the filereference class, but with internal data, rather than the user picking a file. What would be ideal would be direct access to the data and filename vars in place of the browse() method, but that's not supported that I can see.
I have somewhat of a hack working (code below), where I base64 encode the bytearray and send it as the value of a POST var. I can handle this on the ASP end, but I'm thinking there's probably a cleaner way to do this built into flex that I'm missing. I need to send several other variables along with the image, so doing a binary transfer won't really work.
Is there something I'm missing or a simpler way to do this?
//write the canvas to a bitmap
var finalBitmap:BitmapData = new BitmapData(MainCanvas.width,MainCanvas.height);
// write the bitmap to a bytearray
var jpgEncoder:JPEGEncoder = new JPEGEncoder();
var imgArray:ByteArray = jpgEncoder.encode(finalBitmap);
trace("Arr len: " + imgArray.length);
// encode the array for web transfer
var imgEncoder:Base64Encoder = new Base64Encoder();
imgArray.position = 0;
// set up the variables for the POST
var uploadVars:URLVariables = new URLVariables();
uploadVars.user_name = "Test+File";
uploadVars.final_file = imgEncoder.flush();
// prep the request
imgUploadRequest = new URLRequest(FILE_UPLOAD_URL);
imgUploadRequest.method = URLRequestMethod.POST;
imgUploadRequest.data = uploadVars;
// and send it, setting up listeners for results. TODO -- Add a timeout listener
imgUploader = new URLLoader();
imgUploader.dataFormat = URLLoaderDataFormat.VARIABLES;
//imgUploader.addEventListener(IOErrorEvent.IO_ERROR, UploadFinal_Error); imgUploader.load(imgUploadRequest);