Flashbuilder 4.5, Actionscript 3, WSDL: Strange result when encoding received bytearray/string
I am using a C# Soap Webservice, and using this I am trying to sent an image to Actionscript (I am using Adobe Air). I can return this binary data in two ways, return a String on the C# side, or a byte[]. Both give the same result (except that string has “” around it). When I test the operation in Flash Builder, I also get the exact same String.
However, when I try to encode this string into Binary64Data, which is what it officially is in the xml description, I get an odd result. Both Strings should be the same, but the later half of the encoded String is different. Eventually I want to get a ByteArray out of it, which I can use to create my image. This works perfectly with the String version, but the other version returns a half-ruined (but recognizable) image.
What could my problem be? Here is my code, when using a string:
// This works:
var data:String = getBinaryString(); // Returns the result from webservice call token.lastResult
// Remove the two quotes
data = data.substr(1);
data = data.substr(0, data.length-1);
// Decode the base64 string
var dec : Base64Decoder = new Base64Decoder();
dec.decode(data);
imgByteArray = dec.toByteArray();
// Load the image:
loader = new Loader();
loader.loadBytes(bytes);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
// in the event handler:
var bmp:Bitmap = Bitmap(loader.content);
// Some misc stuff, but eventually an image shows, the correct image
But when I try the webservice method that should return a ByteArray:
var data = getBinaryData(); // returns the result from webservice again
var enc : Base64Encoder = new Base64Encoder ();
enc.insertNewLines = false;
enc.encode(data);
var dataString:String = enc.flush(); // also tried toString(), same result
// This dataString should be eactly the same as the above datastring,
// but for some reason, the开发者_Python百科 second half of the string is entirely different,
// while the first half is exactly the same
Is there something I am doing wrong? Preferably I would want to skip the entire encoding/decoding, and just use the ByteArray the webservice call is supposed to return in the first place, but when I try to cast it as such:
var data:ByteArray = getBinaryData() as ByteArray;
Data becomes null. This same data can be transformed with the above sequence, so the data is not empty.
How can I solve my problem? Again, please note that in Flash Builder, using the test operation facilities, both the String as the ByteArray webservice calls return the same value, it is only in the Actionscript 3 that my values diverge.
If what you want to do is convert an object to a ByteArray, you need to call the ByteArray method convertObject(). Try this:
var data:ByteArray = new ByteArray();
data.convertObject(getBinaryData());
精彩评论