URLLoader如何获取已加载的URL?

发布于 2024-12-06 08:25:49 字数 541 浏览 2 评论 0原文

使用 URLLoader 是否可以获取已加载文件的文件名?

public function loadCSS():void {
    var urlLoader:URLLoader = new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
    urlLoader.load(new URLRequest("cssFile1"));
    urlLoader.load(new URLRequest("cssFile2"));
    urlLoader.load(new URLRequest("cssFile3"));
}

private function urlLoader_complete(evt:Event):void {

    // *****How can I get the file name here?
    var css:String = URLLoader(evt.currentTarget).data;
    // Do lots of stuff

}

Using the URLLoader is there anyway to get the filename of the file that has been loaded?

public function loadCSS():void {
    var urlLoader:URLLoader = new URLLoader();
    urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
    urlLoader.load(new URLRequest("cssFile1"));
    urlLoader.load(new URLRequest("cssFile2"));
    urlLoader.load(new URLRequest("cssFile3"));
}

private function urlLoader_complete(evt:Event):void {

    // *****How can I get the file name here?
    var css:String = URLLoader(evt.currentTarget).data;
    // Do lots of stuff

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-12-13 08:25:49

首先,由于 load 方法是异步的,因此代码中的这三个调用将连续相互覆盖。唯一会导致调度 COMPLETE 事件的调用是最后一个调用。如果要异步加载文件,则需要为每个文件创建一个 URLLoader 实例。

其次,(以及您的问题的更多内容) URLLoader 类,允许您访问最初调用 load() 时使用的 URLRequest

解决这个问题的一个简单方法是扩展 URLLoader。例如,如果您只需要 url:

public class MyURLLoader extends URLLoader
{
    private var _url:String;

    public function MyURLLoader(request:URLRequest=null)
    {
        super(request);
    }

    override public function load(request:URLRequest):void
    {
        super.load(request);
        _url = request.url;
    }

    public function get url():String
    {
        return _url;
    }
}

那么在您的代码中您仍然可以使用单个事件处理程序:

public function loadAllCSS():void {
    loadCSSFile("cssFile1");
    loadCSSFile("cssFile2");
    loadCSSFile("cssFile3");
}

private function loadCSSFile(cssURL:String):void {
    var urlLoader:MyURLLoader = new MyURLLoader();
    urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
    urlLoader.load(new URLRequest(cssURL));
}

private function urlLoader_complete(evt:Event):void {
    var cssURL:String = evt.target.url;  //now I know where this came from
    var css:String = evt.data;
}

First of all, since the load method is asynchronous, those three calls in your code are going to override each other in succession. The only call that will lead to the COMPLETE event being dispatched would be the final one. If you want to load the files asynchronously, you need to create an instance of URLLoader for each one.

Second, (and more to your question) there are no properties in the URLLoader class that allow you to access the URLRequest that a load() was initially called with.

A simple way around this would be to extend URLLoader. Eg, if you only needed the url:

public class MyURLLoader extends URLLoader
{
    private var _url:String;

    public function MyURLLoader(request:URLRequest=null)
    {
        super(request);
    }

    override public function load(request:URLRequest):void
    {
        super.load(request);
        _url = request.url;
    }

    public function get url():String
    {
        return _url;
    }
}

Then in your code you could still use a single event handler:

public function loadAllCSS():void {
    loadCSSFile("cssFile1");
    loadCSSFile("cssFile2");
    loadCSSFile("cssFile3");
}

private function loadCSSFile(cssURL:String):void {
    var urlLoader:MyURLLoader = new MyURLLoader();
    urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
    urlLoader.load(new URLRequest(cssURL));
}

private function urlLoader_complete(evt:Event):void {
    var cssURL:String = evt.target.url;  //now I know where this came from
    var css:String = evt.data;
}
爱的十字路口 2024-12-13 08:25:49

创建三个 URLLoader。在完整的函数中,您可以检查事件目标的标识,以确定您从哪一个上获取事件,这将告诉您加载了哪个文件。您也可以使用三个不同的处理程序,具体取决于您想要如何分解代码。

文档并不清楚当您在同一个 URLLoader 上多次调用 load 时会发生什么,这(对我来说)意味着它不是明确定义的行为,您应该避免它。对于您的示例,文档没有指定您的事件处理程序是被调用一次还是三次,以及如果被调用多次,数据是否每次都会不同。

Create three URLLoaders. In the complete function, you can check the identity of the event target to determine which on you're getting the event from, which will tell you which file is loaded. You could also have three different handlers instead, depending in how you want to factor the code.

The docs aren't clear on what happens when you call load multiple times on the same URLLoader, which (to me) means it's not well-defined behavior and you should avoid it. For your example, the documentation doesn't specify whether your event handler will be called once or three times, and if it is called multiple time whether the data will be different each time or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文