Skip to main content
March 17, 2010
Answered

byteArray to cffile binary file

  • March 17, 2010
  • 1 reply
  • 1309 views

Hello,

I having trouble trying to write AS3 to take a screenshot of a MC that’s on the stage and pass the data to a .cfc method which saves it as a jpg on the server.  The current code I have kind of works except the jpg is unreadable.  I think it has something to do with not using Flex, AMF, and writing binary files.  When I look at the jpg via a hex viewer I think it doesn’t have the right jpg header. 

The jpg begins with:
c3 bf c3 98 c3 bf c3 a0 00 10 4a 46

When I think it should begin with:
ff d8 ff e0 00 10 4a 46

//AS3 source code

import flash.net.NetConnection;
import flash.net.Responder;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.utils.ByteArray;
import com.dynamicflash.util.Base64;
import com.adobe.images.JPGEncoder;


var myService:NetConnection = new NetConnection();
var myStruct:Object = new Object;  //for passing arguments to cfc
var hostSite:String;

hostSite = "test.somedomain.com";
myService.objectEncoding = 0;
myService.connect("http://" + hostSite + "/flashservices/gateway")

//sketch_mc is source MC we would like to take a snapshot of, which is already on the stage.
var jpgSource:BitmapData = new BitmapData (sketch_mc.width, sketch_mc.height);
jpgSource.draw(sketch_mc);
var imgEncoder = new JPGEncoder(90);
var jpgStream:ByteArray = imgEncoder.encode(jpgSource);

saveImg(jpgStream);

function saveImg(jpg:ByteArray){
trace("Sending image to cfc");
var encoded:String = Base64.encode(jpg.toString());
myStruct.jpgDataString = encoded;

var responder:Responder = new Responder(saveImg_Result, onFault);
myService.call("latestVersion.secure.saveImage", responder, myStruct);
}
function saveImg_Result(result){
trace("Image Sent Result: "+result)
}
function onFault(f){
trace("Error: " + f.description);
}

<!--- .cfc source --->   
<cfcomponent>
  
    <cffunction name="saveImage" output="no" access="remote" returntype="any" >
        <cfargument name="jpgDataString" type="string" required="yes">
         <cfset img = toBinary(arguments.jpgDataString)>
        <cffile action="write" accept="image" file="d:\test.jpg" output="#img#" nameconflict="overwrite"/> 
        <cfreturn "Image Saved">
    </cffunction>


</cfcomponent>

This topic has been closed for replies.
Correct answer

Think you may want to use Base64.encodeByteArray in your saveImg function.

var encoded:String = Base64.encodeByteArray(jpg)

myStruct.jpgDataString = encoded;

On the server end, you'd need to decode the base64 string. I assume that's what this does:

<cfset img = toBinary(arguments.jpgDataString)>

1 reply

Correct answer
March 17, 2010

Think you may want to use Base64.encodeByteArray in your saveImg function.

var encoded:String = Base64.encodeByteArray(jpg)

myStruct.jpgDataString = encoded;

On the server end, you'd need to decode the base64 string. I assume that's what this does:

<cfset img = toBinary(arguments.jpgDataString)>