As3的SandBox这点上有点让人感觉非常郁闷。
如果要取不同域上的文件(比如歌曲,图片等),当对方服务器没有crossdomain.xml的时候就会出现安全沙箱错误。
这点有点郁闷。
中午午饭后,想到这个问题,于是用Asp.Net写了个Web Proxy。通过Asp.Net进行资源的转发,解决了跨域的安全沙箱问题。
不过这个东东会加大服务器的带宽和计算压力~
另外还有Kingfo同学的一种方法 一种猥琐的图片跨域的方法
上代码(示例尾随其后)
redir.ashx:
<%@ WebHandler Language="csharp" Class="redir" %>
using System;
using System.Web;
using System.IO;
using System.Net;
public class redir : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request["redir"] != null)
{
context.Response.ClearContent();
WebClient client = new WebClient();
byte[] bytes = client.DownloadData(new Uri(context.Request["redir"].ToString()));
if (bytes.Length == 0)
{
context.Response.ContentType = "text/plain";
context.Response.Write(context.Request["redir"].ToString() + "not loaded");
}
else
{
String suffix = context.Request["redir"].ToString().Substring(context.Request["redir"].ToString().LastIndexOf("."));
switch (suffix.ToLower())
{
case ".flv": context.Response.ContentType = "video/x-flv"; break;
case ".mp3": context.Response.ContentType = "audio/mpeg"; break;
case ".gif": context.Response.ContentType = "image/gif"; break;
case ".jpg": context.Response.ContentType = "image/jpeg"; break;
case ".png": context.Response.ContentType = "image/png"; break;
}
context.Response.BinaryWrite(bytes);
}
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
flashclient.as
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.*;
import flash.net.URLRequest;
import flash.text.TextField;
/**
* ...
* @author Dada
*/
public class flashclient extends MovieClip
{
private var loader:Loader;
public function flashclient()
{
btnDownLoad.addEventListener(MouseEvent.CLICK, startDownLoad);
}
private function startDownLoad(mevent:MouseEvent):void
{
loader = null;
loader = new Loader();
txtStatus.text = "";
loader.addEventListener(IOErrorEvent.IO_ERROR, function(ioevent:IOErrorEvent)
{
txtStatus.text = "Io Error";
});
loader.addEventListener(Event.COMPLETE, function(event:Event)
{
txtStatus.text = "Load Complete";
});
loader.load(new URLRequest("redir.ashx?redir="+txtUrl.text));
addChild(loader);
loader.x = 30;
loader.y = 30;
}
}
}
达达你好,有空上AIRIA论坛(BBS.AIRIA.CN)来交流。
@FLEX 好的:)会常来的~呵呵~