Loading a module from outside the application directory in AIR

That has taken a fair bit of Googling to figure that one out. Because of security sandbox restrictions, when using the url of the ModuleLoader component, modules can only be loaded from the application directory (or any of its subdirectories).

Solution adapted from Enable Local App to load RunTime CSS.swf from Server


private function loadModule():void
{
var file:File = File.userDirectory.resolvePath("test/MyModule.swf");
var urlRequest:URLRequest = new URLRequest("file://" + file.nativePath);
var loader:URLLoader;
loader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, onLoaderComplete);
loader.load(urlRequest);
}

private function onLoaderComplete(event:Event):void
{
var loader:URLLoader = event.target as URLLoader;
loader.removeEventListener( Event.COMPLETE, onLoaderComplete);
var myModuleBytes:ByteArray = ByteArray(URLLoader(event.target).data);
propLoader.loadModule("test/MyModule.swf", myModuleBytes);
}

Providing a null url parameter for loadModule triggers and error. Providing an empty string "" or a file that doesn't exist in the application directory like "test/MyModule.swf" appears to work. I would need to figure out why.

Other solutions:

Powered by Drupal, an open source content management system