Sending objects between flash movies with POST
From Birnam Designs Wiki
It is possible to send entire objects between Flex and Flash movies via POST using ByteArray and Base64Encoder/Base64Decoder.
Note: The Base64Encoder/Base64Decoder classes are in the mx.utils package. If you are making a non-Flex interface, there are third party Base64 classes available.
The key to restoring the information as a class instead of a simple Object is the registerClassAlias and getQualifiedClassName functions. These allow the class information to be retained. Otherwise, the readObject function will successfully output variables in a simple object, but if you tried to cast this to a class type, it will result in a null variable.
Data Class
As far as I can tell, this can be any type of class. The entire state is stored.
package { public class DataClass { public var a:Number; public var b:String; public var c:Boolean; public function DataClass() { } } }
Sender Class
package { import mx.utils.Base64Encoder; import flash.utils.getQualifiedClassName; import flash.net.registerClassAlias; import flash.net.URLRequest; import flash.net.URLRequestMethod; import flash.net.URLVariables; public class Sender { public function Sender() { // create DataClass object to use, // and register its class using its // qualified class name var dc:DataClass = new DataClass(); // necessary so flash will know how to encode as a class registerClassAlias(getQualifiedClassName(cbdata), MHouseData); // create a ByteArray from the object var ba:ByteArray = new ByteArray(); ba.writeObject(cbdata); // create a Base64 string from the ByteArray var b64:Base64Encoder = new Base64Encoder(); b64.encodeBytes(ba); var b64string:String = b64.toString(); // sent it to the receiving page var u:URLRequest = new URLRequest("nextswf.php"); var v:URLVariables = new URLVariables(); v.dcdata = b64string; u.data = v; // since it's a string, you can (with only some limitations) // use GET instead, but it does look like a big // junk string on the end of the url! u.method = URLRequestMethod.POST; navigateToURL(u, "_self"); } } }
Receiving Class
package { import mx.utils.Base64Decoder; import flash.utils.getQualifiedClassName; import flash.net.registerClassAlias; public class Receiver { // this assumes you have used PHP or other // to add the Base64 string to FlashVars. // -- don't forget, it will probably be necessary to // urlencode or equivalent to make sure the // Base64 string is passed correctly to flash var dcdata:String = Application.application.parameters.dcdata; // must create an instance to retreive the classname var dc:DataClass = new MHouseData(); // necessary so flash will know how to decode as a class registerClassAlias(getQualifiedClassName(cbdata), MHouseData); // decode the Base64 string var b64:Base64Decoder = new Base64Decoder(); b64.decode(dcdata); // save back out to ByteArray var ba:ByteArray = b64.toByteArray(); // export as an object var obj:Object = ba.readObject(); // ensure that it is casted as the DataClass dc = obj as DataClass; } }