首先 ,说明我测试的系统地址和网络地址的情况:
某电脑一:file://E:\vcs\HelloWorld\bin-debug\player.swf
某电脑二:file://D:/vcs/HelloWorld/bin-debug/player.swf
由于两者的不同,下面程序在发布之后进行了修改。
但要明确的是无论什么系统开发,网络程序在运行时,获取的服务器地址都是
http://www.baidu.com/mp3/...形式的,即地址分隔符均为“/”
1、在AS项目中获取当前项目编译的SWF所在的服务器路径或本地路径
将下面的代码放在AS项目的最上层类中
- package {
- import flash.display.Sprite;
- public class HelloWorld extends Sprite
- {
- public static var currSwfUrl:String;
- public function HelloWorld(){
- //写网络相关的项目时,就可以通过这个自动选择调用的服务器端程序了
- var doMain:String = this .stage.loaderInfo.url;
- var doMainArray:Array = doMain.split("/" );
- if (doMainArray[ 0 ] == "file:" ) {
- //为处理本地系统返回的路径由“/”或“\”两种间隔组成的不同情况,而分别处理
- if (doMainArray.length<= 3 ){
- currSwfUrl = doMainArray[2 ];
- currSwfUrl = currSwfUrl.substring(0 ,currSwfUrl.lastIndexOf(currSwfUrl.charAt( 2 )));
- }else {
- currSwfUrl = doMain;
- currSwfUrl = currSwfUrl.substring(0 ,currSwfUrl.lastIndexOf( "/" ));
- }
- }else {
- currSwfUrl = doMain;
- currSwfUrl = currSwfUrl.substring(0 ,currSwfUrl.lastIndexOf( "/" ));
- }
- currSwfUrl = currSwfUrl + "/" ;
- trace("项目路径: " + HelloWorld.currSwfUrl);
- }
- }
- }
package {
import flash.display.Sprite;
public class HelloWorld extends Sprite
{
public static var currSwfUrl:String;
public function HelloWorld(){
//写网络相关的项目时,就可以通过这个自动选择调用的服务器端程序了
var doMain:String = this.stage.loaderInfo.url;
var doMainArray:Array = doMain.split("/");
if (doMainArray[0] == "file:") {
//为处理本地系统返回的路径由“/”或“\”两种间隔组成的不同情况,而分别处理
if(doMainArray.length<=3){
currSwfUrl = doMainArray[2];
currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf(currSwfUrl.charAt(2)));
}else{
currSwfUrl = doMain;
currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/"));
}
}else{
currSwfUrl = doMain;
currSwfUrl = currSwfUrl.substring(0,currSwfUrl.lastIndexOf("/"));
}
currSwfUrl = currSwfUrl + "/";
trace("项目路径: "+ HelloWorld.currSwfUrl);
}
}
}
输出结果:“ 项目路径: E:\vcs\HelloWorld\bin-debug/ ”
2、在Flex Web项目中获取当前项目编译的SWF所在的服务器路径或本地路径
把下面的代码放在Flex的App类中即可使用
- <?xml version= "1.0" encoding= "utf-8" ?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout= "absolute" initialize= "initApp()" >
- <mx:Script>
- <![CDATA[
- public static var currSwfUrl:String;
- private function initApp(): void {
- //写网络相关的项目时,就可以通过这个自动选择调用的服务器端程序了
- var doMain:String = this .parent.stage.loaderInfo.url; //注意此行和AS的写法略有不同
- var doMainArray:Array = doMain.split("/" );
- if (doMainArray[ 0 ] == "file:" ) {
- if (doMainArray.length<= 3 ){
- //为处理本地系统返回的路径由“/”或“\”两种间隔组成的不同情况,而分别处理
- currSwfUrl = doMainArray[2 ];
- currSwfUrl = currSwfUrl.substring(0 ,currSwfUrl.lastIndexOf(currSwfUrl.charAt( 2 )));
- }else {
- currSwfUrl = doMain;
- currSwfUrl = currSwfUrl.substring(0 ,currSwfUrl.lastIndexOf( "/" ));
- }
- }else {
- currSwfUrl = doMain;
- currSwfUrl = currSwfUrl.substring(0 ,currSwfUrl.lastIndexOf( "/" ));
- }
- currSwfUrl += "/" ;
- trace("项目路径: " + HelloFlex.currSwfUrl);
- }
- ]]>
- </mx:Script>
- </mx:Application>