为什么URLReqeusts不会加载我的文件时,我将它们移动到一个Web服务器? [英] Why do URLReqeusts not load my files when I move them to a web server?

查看:107
本文介绍了为什么URLReqeusts不会加载我的文件时,我将它们移动到一个Web服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种更好的方式我使用可能处理这个问题它只是空话,但我没有找到我想要的答案。我会解释我在做什么,希望有人能能够解释如何做到这一点。

在开发我的机器上的闪存项目,我可以用这样的:

  VAR要求:的URLRequest =新的URLRequest(images.xml);
        VAR装载机:的URLLoader =新的URLLoader();
        loader.addEventListener(引发Event.COMPLETE,handleResult);
        loader.addEventListener(IOErrorEvent.IO_ERROR,handleIOError);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR,handleSecurityError);

        loader.load(要求);
 

只要images.xml文件位于同一目录中的.fla然后正常工作。然而,一旦我将.swf文件,以及名为.xml我的图片到网上这一切都付诸东流。瑞士法郎再也找不到.xml文件,我知道这是为什么。使用FlashBug我可以读取控制台的错误。

  IO错误:[IOErrorEvent TYPE =ioError在泡沫=假可取消=假的EventPhase = 2文本=错误#2032:流错误的URL:http://www.mydomain。 COM / images.xml]
 

和有问题,当我把我的瑞士法郎到我的Web服务器时,URLRequest增加了我的域名请求。由于文件只是设在我的机器上相同的目录并没有造成问题,只是说images.xml,但现在为了使它工作,我必须改变它的http://www.mydomain .COM /闪光灯/ MyProject的/ images.xml。

我想知道是否有办法迫使瑞士法郎,只是继续关注它是因为它需要的文件的目录。我认为有拼出来是反直觉的,因为那时我不能移动的文件,而无需重新编写我的AS3 code。

我知道这是可以做到我所见过的项目做到这一点,我只是希望我知道他们是如何做到的。

解决方案
  

我想知道是否有办法迫使瑞士法郎,只是继续   看在它是因为它需要的文件的目录。

是的,有,你可以使用基础的对象和嵌入标签的属性。

由于默认情况下,在ActionScript相对路径解析相对于包含Flash,而不是相对于SWF文件的网页,但基础属性可以让你指定其他目录路径,用于解析相对路径

因此​​,对于上面的示例路径,这将是这样的:

 < PARAM NAME =基地值=/闪光灯/ MyProject的/>

<嵌入底座=/闪光灯/ MyProject的/SRC =...
 

或者如果你使用SwfObject的嵌入到SWF,您可以通过指定的属性参数库:

HTTP://$c$c.google.com/p/ SWFObject的/维基/文档

修改

在这方面的一个后续,虽然答案已被接受,但既然你评论说,你在哪里开始寻找处理它在ActionScript而不是HTML,并为未来可能的读者:

如果你出于某种原因不愿或不能,设置基础中的HTML属性,在ActionScript另一种方法是使用与LoaderInfo.url 得到的SWF的URL,挑的目录部分,并用它作为路径来加载的项目旁边的SWF文件。这是code,从我的一个项目,包括意见复制:

  // URL到主SWF文件从装载目录(文件夹)。
//可用于加载其他文件realtive(而不是realtive的页面,这是默认值),以瑞士法郎。
//所以swfDir会normaly像http://www.site.com/upload/somefolder/
变种swfDir:字符串= loaderInfo.url.substring(0,loaderInfo.url.lastIndexOf(/)+1);
 

和以后,当你要加载例如一个XML文件,你会怎么做:

  VAR要求:的URLRequest =新的URLRequest(swfDir +images.xml);
 

I have been looking for a better way to handle this issue maybe its just the verbiage I'm using but I'm not finding the answer I want. I'll explain what I'm doing and hopefully someone out there will be able to explain how to do this.

When developing a flash project on my machine I can use something like:

        var request:URLRequest = new URLRequest("images.xml"); 
        var loader:URLLoader = new URLLoader();
        loader.addEventListener(Event.COMPLETE, handleResult);
        loader.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
        loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, handleSecurityError);

        loader.load(request);

As long as the images.xml file is located in the same directory as the .fla then it works fine. However once I move the .swf, .xml and my images to the web it all goes down the drain. The .swf can no longer find the .xml file and I know why. Using FlashBug I can read the error in the console.

IOError: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://www.mydomain.com/images.xml"]

And there's the problem when I move my .swf to my web server the URLRequest adds my domain to the request. Since the file was just located in the same directory on my machine it didn't cause an issue to just say "images.xml" but now in order to make it work I have to change it to "http://www.mydomain.com/flash/myproject/images.xml".

I want to know if there is a way to force the .swf to just continue to look in the directory it is in for the files it needs. I think having to spell it out is counter-intuitive because then I can't just move the files without having to re-write my AS3 code.

I know this can be done I've seen projects do it, I just wish I knew how they did it.

解决方案

I want to know if there is a way to force the .swf to just continue to look in the directory it is in for the files it needs.

Yes, there is, you can use the base attribute of the object and embed tags.

As default, relative paths in ActionScript are resolved relative to the page that contains the Flash, not relative to the swf file, but the base attribute lets you specify another directory path, that is used to resolve relative paths.

So for your example path above, it would be something like this:

<param name="base" value="/flash/myproject/">

<embed base="/flash/myproject/" src="...

Or if you use SwfObject to embed the swf, you can specify base via the attributes parameter:

http://code.google.com/p/swfobject/wiki/documentation

Edit:

A followup on this, although the answer has already been accepted, but since you comment that you where initially looking to handle it in ActionScript instead of HTML, and for possible future readers:

If you for some reason don't want to, or can't, set the base attribute in the HTML, an alternative in ActionScript is to use loaderInfo.url to get the URL of the swf, pick the directory part of that and use it as a path to load files that are next to the swf. This is code copied from a project of mine, including the comments:

// URL to the directory (folder) the main swf file is loaded from.
// Can be used to load other files realtive to the swf (instead of realtive to the page, which is default).
// So swfDir will normaly be like "http://www.site.com/upload/somefolder/"
var swfDir:String = loaderInfo.url.substring(0, loaderInfo.url.lastIndexOf("/")+1);

And later, when you want to load for example an XML file, you would do:

var request:URLRequest = new URLRequest(swfDir + "images.xml");

这篇关于为什么URLReqeusts不会加载我的文件时,我将它们移动到一个Web服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆