Search code examples
flashactionscript-3externalinterface

Flash ExternalInterface.addCallback error


So I have one function:

public function handleOnSave(e:MouseEvent):void
        {
            var ml:MultipartURLLoader = new MultipartURLLoader();
            var bmData:BitmapData = scrollView.getBitmapData();
            ml.addEventListener(Event.COMPLETE, onSaveComplete);

            // simple string data
            ml.addVariable("ad_id", ParamsObject.lastInstance.param_id);
            ml.addVariable("ad_type", ParamsObject.lastInstance.param_type);
            ml.addVariable("image_width", bmData.width);
            ml.addVariable("image_height", bmData.height);
            ml.addVariable("save_data", scrollView.getSavedData());

            trace(scrollView.getSavedData());
            var jpgSource:Bitmap = null;

            var jpgEncoder:JPGEncoder = new JPGEncoder(99);
            var jpgStream:ByteArray = jpgEncoder.encode(scrollView.getBitmapData());
            var pngStream:ByteArray = PNGEncoder.encode(bmData);
            ml.addFile(jpgStream, 'test2.jpg', "pic" , 'image/jpeg');
            trace(pngStream.length + ' ' + jpgStream.length );
            ml.load("/flash/uploadFP.php");
        }

I have button to trigger it like:

save_btn.addEventListener(MouseEvent.CLICK, handleOnSave);  

What I want to do is to put this button outside flash and to call this function with Java Script using ExternalInterfacе like:

ExternalInterface.addCallback("save",handleOnSave);

but the browser returns

uncaught exception: Error in Actionscript. Use a try/catch block to find error.

I try to call function which only return alert for success and it worked. I searched for this and the only thing which people suggest is to use

Security.allowDomain("*");

I did it but the problem remains.

Edited:

OK try/catch returned Error #2176!


Solution

  • First thing noticed is

     public function handleOnSave(e:MouseEvent):void ...
    

    has event e as parameter, which you won't be able to pass from Javascript.

    As e is not used inside handleOnSave you can define the function like

      public function handleOnSave(e:MouseEvent = null):void ...
    

    So in Actiobscript, it should be like

      public function handleOnSave(e:MouseEvent = null):void{
       .....
       .....
      }
    
      ExternalInterface.addCallback("save",handleOnSave);
    

    and from Javascript you can call something like

      document[*flash movie id*].save();
    

    Please share your Javascript code to see whether anything else is wrong.

    EDIT:

    As it shows to find out the error

     public function handleOnSave(e:MouseEvent = null):String{
            try{
    
                ...existing code...
    
            }catch(error:Error){
    
                return error.message;
    
            }
    
            return "";
     }
    

    and receive the error in Javascript.