问题
I need to be able to load a jpeg/png image from disk and show it in flex and send it to a server as a base64 encoded string. But once the image file is loaded, in my flash.display.LoaderInfo
object, the bytes
property (type of ByteArray
) contains more byte than the file content.
Example: image file size: 3089 flash.display.LoaderInfo.bytesTotal:3089 flash.display.LoaderInfo.bytes.length:3155
As i need to encode the flash.display.LoaderInfo.bytes
in base64 string, i don't know which part of the ByteArray objet i must send to server.
I don't want to draw the bytearray content into a Bitmap image and re-encode it as jpg because i must keep the original quality of the file.
Thanks
some code:
private function onDataLoadComplete(event:Event):void {
var encoder:Base64Encoder = new Base64Encoder();
//var imagePartBytes:ByteArray = new ByteArray();
//imagePartBytes.writeBytes(event.target.bytes, 0, event.target.bytesTotal);
//imagePartBytes.writeBytes(event.target.bytes, 0, event.target.bytes.length);
//imagePartBytes.writeBytes(event.target.bytes, event.target.bytes.length-event.target.bytesTotal, event.target.bytesTotal);
encoder.encodeBytes(event.target.bytes);
var imagePart:String = encoder.flush();
trace(imagePart);
data = fileName+";"+event.target.contentType+";"+imagePart;
_changed = true;
}
回答1:
Patrick thank you for your response. I found a solution. I was using a FileReference to load an image directly to flash player and than using a Loader to load the image to the stage. I was trying to encode the Loader's ByteArray istead of the FileReference.data which is a ByteArray, the one i need :)
var encoder:Base64Encoder = new Base64Encoder();
encoder.encodeBytes(fileRef.data);
imagePart = encoder.flush();
and it works.
回答2:
Normally you should send all the content of the bytearray. How are you loading the image, are you sure you are receiving data in binary form ? If you display the image loaded is there any problem ?
If you encode data with base64, the resulting data length will be greater than the original since you reencode 8 bits long data with 6 bits. So for one byte it will fit in 2 bytes.
来源:https://stackoverflow.com/questions/1927163/flash-loader-and-bytearray