Search code examples
csvactionscript-3flashactionscript

ActionScript 3.0 .fla file working, .swf file exported not working


I have another problem with an acrionscript swf file. I'm reading some text from a CSV file. I create the fla file and when I test it in the animate editor it works and I can see all the text inside the CSV file into a TextField. If I export the .swf and run it with Adobe Flash Player 11.4 r402 I can't see the text that there is in the CSV File. I created this fla file made up of one photogram in which I wrote this code:

import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.filesystem.*;
import flash.text.TextField;

var pathToFile:String = File.applicationDirectory.resolvePath('ex.csv').nativePath;

var requestedFile:URLRequest = new URLRequest(pathToFile);

var myTextField:TextField = new TextField();

myTextField.autoSize = TextFieldAutoSize.LEFT;

var documentLoader = new URLLoader();

documentLoader.addEventListener(Event.COMPLETE, onload);

documentLoader.load(requestedFile);

function onload(evt:Event):void
{
  myTextField.text = evt.target.data;
  addChild(myTextField);
}

I get this error if I try to open the swf file in a broswer:

SecurityError:[SecurityErrorEvent type ="securityError" bubbles = false cancelable = false evenPhase=2 text="Error#2048"

How can I make the .swf file work? Is there a way to read data from a CSV file into a .swf file?

Thank you to everybody!


Solution

  • This is most likely a sandbox issue (security error).

    To know what's going on, you should always listen for errors (instead of just COMPLETE) when you use asynchronous classes like URLLoader.

    Add the following listeners in addition to your complete listener:

    documentLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError); 
    documentLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); 
    

    And add the respective handler methods:

    function onIOError(e:IOErrorEvent):void {
        //file not found, the event (e) will tell you the path it was trying to load and other details
        trace(e);
        //alert the user there was a problem
    }
    
    function onSecurityError(e:SecurityErrorEvent):void {
        //file was not allowed to load, sandbox issue
        trace(e);
        //alert the user there a problem
    }
    

    If you are indeed getting a security/sandbox error as I suspect, make sure you are publishing with the correct security sandbox setting.

    Go to your publish settings file -> publish settings.

    You'll see a drop down labelled Local playback security. Ensure this is set to access network only and not the default access local only (if your CSV is coming from a server).