from:http://www.cnblogs.com/qkaibo/archive/2007/07/28/834238.html
?
关于flex跨域读取一个图片
比如? 假设你的服务器事??www.myserver.com??? 而你的一个flex文件位于??www.myserver.com?/flex/myfalsh.swf
当一个客户来访问你的这个myfalsh.swf 文件? 而你的这个文件又要去www.otherserver.com/img/1.jpg? 这里把这个图片加载到flash中
怎么办? 最简单的方法就是 var l:loader = new loader; l.load(new urlrequest(""));??? 这个方式如果你的flash是直接放在本地目录上运行那还可以
放在服务器上? 加载 立马 报错?安全沙箱冲突
看了看loader 的 load的方法load(request:URLRequest,context:LoaderContext?= null):void?
context:LoaderContext? 这个干吗用的? 再看看帮助 原来是用来设置运行域 和 设置是否加载安全策略文件的
当然使用方法是? var lc:LoaderContext?= new?LoaderContext(true);
?var l:loader = new loader; l.load(new urlrequest(""),lc);????这样行吗? 一样不行
还要放一个 安全策略文件 放在对方服务器的根目录下面? 名称为:crossdomain.xml 最好是这个名字 省得多写代码
内容为
<?xml?version="1.0"?>
<cross-domain-policy>
??<allow-access-from?domain="?www.myserver.com"?/>
</cross-domain-policy>
一般将安全策略文件放在空的WebContent工程下,与WEB-INF同级目录。将打包后的工程代码,放在另一方的服务器中即可!
所以这种方式有个很大弊病 就是要在对方服务器上放这么一个文件,别人还肯给你放啊 ? 要不就是傻了
第2种方式 很自然的就是想到用代理方法? 就是 用asp,php 等类似的程序去把这种图片读回来 然后传给flex??
具体: 放一个如 getpic.asp 在服务器上 和myfalsh.swf 同一个目录
getpic.asp的代码为
<%
function?reReader(url)?
dim?http
set?http?=?server.CreateObject("Microsoft.XMLHTTP")?
with?http?
.Open?"get",?url,?
false,?"",?""?
.Send?
reReader?=?.ResponseBody
end?
with?
set?http?=?
nothing?
end?function
dim?url
url?=Request.QueryString("url")
response.Clear
Response.ContentType?=?"image/gif"
Response.BinaryWrite?reReader(url)
response.End?
%>
myfalsh.swf 种的代码这样写

private?var?_loader:Loader;

private?var?_LoadUrl:String;

public?function?FileLoader(url:String){

????_LoadUrl?=?url;

????_loader?=?new?Loader();

_loader.contentLoaderInfo.addEventListener(Event.COMPLETE,OnLoadCompleateEvent);

_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,OnLoadIOErrorEvent);

????????}

????????

????????//加载

function?Load():void{

????var?req:URLRequest?=?new?URLRequest(_LoadUrl);

????_loader.load(req);

}

????????

function?get?content():DisplayObject{

????????????return?_loader.content;

????????}

????????

private?function?OnLoadCompleateEvent(e:Event):void{

this.dispatchEvent(new?Event(Event.COMPLETE));

????????}

????????

function?OnLoadIOErrorEvent(e:IOErrorEvent):void{

????Alert.show("加载错误");

????????????//this.dispatchEvent(new?Event(IOErrorEvent.IO_ERROR));

????????}
调用方法
? FileLoader(http://www.myserver.com?/flex/myfalsh.swf?url=http://www.otherserver.com/img/1.jpg); 搞定 ? ??