Select and access local SWF files within an AIR application

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:

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;

6 thoughts on “Select and access local SWF files within an AIR application”

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

    God bless u.

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

    when I use this the loader.content stays null

  3. Nice solution! I have tested on Flash Builder 4.6 and i use Flex SDK 4.12 with 14.x Flash Player Version. It works fine. Wow i never think because swfByte and loaderContext needed yet. And how does it work with systemManager for accessing variables and functions from externed swf files ( swfByte )?

    Is it possible? Thanks, regards, Jens

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.