Next Post Previous Post

17 Jun 2008

Select and access local SWF files within an AIR application

Posted by derRaab

Well, it took me a little time to figure out how to select and load a local SWF file into an SWFLoader instance. Several times I got this error message:

“SecurityError: Error #3015: Loader.loadBytes() is not permitted to load content with executable code.”

After some research I found this "dreaming in flash" blog entry which basically contains the solution I was looking for.

But then it took me a little more time to figure out how to use this with the SWFLoader. So here is a basic code example:

Actionscript:
import flash.display.Loader;
import flash.filesystem.File;
import flash.filesystem.FileMode;
import flash.filesystem.FileStream;
import flash.system.LoaderContext;
import flash.utils.ByteArray;
import mx.controls.SWFLoader;

// File reference
var swfFile: File;

// Open the SWF file
var fileStream: FileStream = new FileStream();
    fileStream.open( swfFile, FileMode.READ );

// Read SWF bytes into byte array and close file
var swfBytes: ByteArray = new ByteArray();
    fileStream.readBytes( swfBytes );
    fileStream.close();

// Prepare the loader context to avoid security error
var loaderContext: LoaderContext = new LoaderContext();
    loaderContext.allowLoadBytesCodeExecution = true; // that's it!
   
// Now you could use this with a Loader instance
var loader: Loader = new Loader();
    loader.loadBytes( swfBytes, loaderContext );
   
// Or you could use this with a SWFLoader instance
var swfLoader: SWFLoader = new SWFLoader();
    swfLoader.loaderContext = loaderContext;
    swfLoader.source = swfBytes;

Subscribe to Comments

4 Responses to “Select and access local SWF files within an AIR application”

  1. Nice example, we’re always glad to help :)

     

    pedro

  2. Thank you ,i am just looking for this. i want to load images ,png,jpeg by ByteArray for SWFLoader.

    God bless u.

     

    Chen.li

  3. Does this still work in the latest Air version?

    when I use this the loader.content stays null

     

    Bart

  4. Hm. I don’t know. Maybe not anymore? Tell us…

     

    derRaab

Leave a Reply

Message: