17 Jun 2008
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.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;
Nice example, we’re always glad to help
pedro
June 18th, 2008 at 12:50 pmpermalink
Thank you ,i am just looking for this. i want to load images ,png,jpeg by ByteArray for SWFLoader.
God bless u.
Chen.li
June 4th, 2009 at 3:06 pmpermalink
Does this still work in the latest Air version?
when I use this the loader.content stays null
Bart
June 7th, 2009 at 9:18 pmpermalink
Hm. I don’t know. Maybe not anymore? Tell us…
derRaab
June 7th, 2009 at 9:43 pmpermalink