本文意在说明产生沙箱的原因和解决办法
swf代码如下:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init()" layout="absolute" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
public function init():void
{
var ldr:Loader = new Loader();
var url:String = "http://192.168.0.50:81/TestRSL/others.jpg";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
// addChild(ldr);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderHander);
}
internal function loaderHander(evt:Event):void{
imgDiv.source=evt.currentTarget.content;
//imgDiv.source=_loader.content;
}
]]>
</mx:Script>
<mx:Image x="58" y="6" width="397" height="296" id="imgDiv" autoLoad="true"/>
</mx:Application>
简要说明一下,通过Loader和URLRequest获取图片赋值Image。
首先在Flash Builder下调试,此时Security.sandboxType为localTrusted,此时是不会出现安全沙箱冲突的。
然后把debug下文件全部拷贝本机(192.168.0.187)的IIS下,此时Security.sandboxType为remote,出现安全沙箱冲突。(192.168.0.50的IIS下没有crossdomain.xml)
如果通过策略文件方式来解决,需要先修改代码:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init()" layout="absolute" minWidth="955" minHeight="600">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function init():void
{
var lc:LoaderContext = new LoaderContext(true);
var ldr:Loader = new Loader();
var url:String = "http://192.168.0.50:81/TestRSL/others.jpg";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq,lc);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderHander);
}
internal function loaderHander(evt:Event):void{
imgDiv.source=evt.currentTarget.content;
Alert.show("You just changed views."+Security.sandboxType);
//imgDiv.source=_loader.content;
}
]]>
</mx:Script>
<mx:Image x="58" y="6" width="397" height="296" id="imgDiv" autoLoad="true"/>
</mx:Application>
主要是load方法添加一个参数,具体文档说明为:
context:LoaderContext
(default = null
) — LoaderContext 对象,它具有定义下列内容的属性:
- 是否应在加载对象时检查策略文件是否存在
- 被加载的对象的 ApplicationDomain
- 加载的对象的 SecurityDomain
- 加载的图像对象的 ImageDecodingPolicy
如果未指定 context
参数或者该参数引用了 null 对象,则已加载的内容将保留在自己的安全域中,所以仅仅添加策略文件时不够的
解决方法1:默认策略文件,就是添加crossdomain.xml到192.168.0.50的IIS跟目录下。
解决方法2:代码中加载策略文件,因为默认策略文件给服务器维护造成影响,可以放到子域中,用Security.loadPolicyFile(),但是这种在flash player10就不行了,必须要先在主策略文件设置,然后才可以