Skip to content

Instantly share code, notes, and snippets.

@kitschpatrol
Created August 11, 2011 15:26
Show Gist options
  • Save kitschpatrol/1139937 to your computer and use it in GitHub Desktop.
Save kitschpatrol/1139937 to your computer and use it in GitHub Desktop.
private function upload(picture:BitmapData, format:String = JPG):void {
var urlLoader:URLLoader = new URLLoader();
// encode image to ByteArray
var byteArray:ByteArray;
// encode in the requested file format
switch (format) {
case JPG:
// encode as JPG
var jpgEncoder:JPGEncoder = new JPGEncoder(98);
byteArray = jpgEncoder.encode(picture);
break;
case PNG:
// encode as PNG
byteArray = PNGEncoder.encode(picture);
break;
default:
trace("error! fell through upload image format switch case");
break;
}
// convert binary ByteArray to plain-text, for transmission in POST data
var encoder:Base64Encoder = new Base64Encoder();
encoder.encodeBytes(byteArray);
// set upload destination
var request:URLRequest = new URLRequest('upload url here');
request.method = URLRequestMethod.POST;
// set data to send
var variables:URLVariables = new URLVariables();
variables.format = format;
variables.image = encoder.toString();
request.data = variables;
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.load(request);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment