as3安全沙箱违规

ast*_*ter 1 flash load sandbox actionscript-3

我已经阅读了所有类似的主题,但没有运气,所以我发布了一个关于此错误的新问题.

我正在尝试使用以下代码从另一个swf文件加载swf文件:

var loader:Loader = new Loader()

//listen for loading progress
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgress);

//listen for when the load is finished
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onLoaderError);

//load!
var rr:URLRequest = new URLRequest("http://localhost/Gen-Tree.swf")
loader.load(rr);


function onLoaderError(event:SecurityErrorEvent) {
    trace("hi")
}

function onProgress(event:ProgressEvent):void
{
    //calculate how much has been loaded
    var percentageLoader:Number = event.bytesLoaded / event.bytesTotal;

    //use your percentage number here to drive a loader bar graphic
}

function onComplete(event:Event):void
{
    //remove listeners now that loading is done
    loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onProgress);
    loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);

    //add loaded swf to the stage
    addChild(loader.content);

}
Run Code Online (Sandbox Code Playgroud)

我收到如下错误消息:

SecurityDomain 'http://localhost/Gen-Tree.swf' tried to access incompatible context 'file:///C|/Users/Alex/Desktop/Gen%2DTree%202012/Programming/loader.swf'
*** Security Sandbox Violation ***
SecurityDomain 'http://localhost/Gen-Tree.swf' tried to access incompatible context 'file:///C|/Users/Alex/Desktop/Gen%2DTree%202012/Programming/loader.swf'
*** Security Sandbox Violation ***
SecurityDomain 'http://localhost/Gen-Tree.swf' tried to access incompatible context 'file:///C|/Users/Alex/Desktop/Gen%2DTree%202012/Programming/loader.swf'
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

red*_*nce 10

您正在尝试从文件系统加载外部swf,这将导致安全性错误.把它放在服务器上,它应该可以正常工作,只要两个swfs在同一个域上.或运行本地服务器并尝试它.

如果两个swfs不在同一个域中,则需要添加一个crossdomain.xml.它将在服务器根目录上运行,它看起来像这样:

<?xml version="1.0"?>
<cross-domain-policy>
    <allow-access-from domain="*" />
</cross-domain-policy>
Run Code Online (Sandbox Code Playgroud)

除了你不应该使用,*因为它会打开你的安全风险.您需要专门将其他域列入白名单.您可以在此处了解有关跨域策略文件的更多信息.

更新:
此外,由于装载机的swf访问它加载(通过内容loader.content),你需要的安全权限添加到内容SWF(貌似叫Gen-Tress.swf):

import flash.system.Security;

Security.allowDomain("*");
Run Code Online (Sandbox Code Playgroud)

值得注意的是,这Loader是一个DisplayObject意思,你可以直接将它添加到stagewith addChild(loader)而不是addChild(loader.content).通过不访问Loader内容,您通常可以避免安全沙箱违规错误,而不必处理允许域和跨域策略.