2011-04-08 94 views
5

嗨, 我想从另一台服务器上将swf文件加载到我的Flash应用程序。 当我尝试加载它在闪存IDE(CRL +进入)一切工作正常,但是当我运行SWF作为一个独立的SWF文件,或通过调试它,我得到这个错误:Flash AS3尝试获取外部swf时违反安全沙箱

SecurityError: Error #2121: Security sandbox violation: LoaderInfo.content: file:///C|/Users/something/Desktop/blablabla/myplayer.swf cannot access http://www.somedomain.com/blablabla/lalalala/abc.swf. This may be worked around by calling Security.allowDomain. 
at flash.display::LoaderInfo/get content() 
at wallplayer_fla::MainTimeline/swfLoaded()[wallplayer_fla.MainTimeline::frame1:216] 
Cannot display source code at this location. 

我在我的服务器的根目录下的crossdomain.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> 
<cross-domain-policy> 
    <allow-access-from domain="*" /> 
</cross-domain-policy> 

在“myplayer.swf”我有:

Security.allowDomain("*"); 
Security.allowInsecureDomain("*"); 
... 
... 
var loaderContext:LoaderContext = new LoaderContext(); 
loaderContext.checkPolicyFile = true; 
loaderContext.allowCodeImport = true; 

ldr = new Loader(); 
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoaded); 
ldr.load(new URLRequest(graySwfFilename), loaderContext); 
... 
... 
var mcExt; 
var ldr:Loader; 
function swfLoaded(e:Event):void { 
    mcExt = MovieClip(ldr.contentLoaderInfo.content); 
    ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, swfLoaded); 
    mcExt.x = 0; 
    mcExt.y = 0; 
    addChild(mcExt); 
} 

我真的不知道该怎么办... 请帮助?

回答

9

解决方案:针对Flex 4.x版(目前4.6)和AS3在Flash Builder:

import flash.system.SecurityDomain; 
    import flash.system.ApplicationDomain; 
    import flash.system.LoaderContext; 
    var loaderContext:LoaderContext = new LoaderContext(); 
    loaderContext.applicationDomain = ApplicationDomain.currentDomain; 
    loaderContext.securityDomain = SecurityDomain.currentDomain; // Sets the security 

上下文来解决错误#2121

...现在

loader.load(new URLRequest(webServerWebURL),loaderContext); 
加载SWF
1

在“全局安全设置”面板中添加您的工作文件夹。这使您可以从文件系统中的单个SWF加载外部文件。如果你希望a.swf加载b.swf,即使它们位于同一个文件夹中,也是如此。

这将使你的SWF阅读外部文件。

如果您将SWF上传到服务器,这应该不是问题。

+0

哇!谢啦! 但它只能解决我的电脑上的问题。如果我要通过互联网上传这个播放器到某个网站,它不会修复它......不是吗? – hdmi3killer 2011-04-08 12:51:13

+0

在网站中实现myplayer.swf时,我可以达到相同的结果吗? – hdmi3killer 2011-04-08 14:24:22

+0

@ hdmi3killer swf有本地到全球的加载限制。一旦在服务器上这些限制不会影响加载 – jolyonruss 2011-04-08 14:46:13

0

您的错误来自没有跨域文件的外部位置,而不是您。看看using a bridge file to communicate with an external api这个教程。我在这个例子中使用了as3和php。诀窍是保持域上跨域文件的控制权。然后让服务器与api进行通信。