Flash AS3 Read Text File

2019-08-05 11:42发布

问题:

I simply need to read a text file from my computer, or a website. I've tried everything, and nothing so far has worked. It should be extremely simple, just reading a text file from a website, like http://foo.com/foo.txt/, but I've tried everything, and nothing I have seen on Google comes even close to working. I don't care how I get the problem solved, as long as I can do it.

回答1:

To read the content of a file, just use a URLLoader:

// Define the path to the text file.
var url:URLRequest = new URLRequest("file.txt");

// Define the URLLoader.
var loader:URLLoader = new URLLoader();
loader.load(url);

// Listen for when the file has finished loading.
loader.addEventListener(Event.COMPLETE, loaderComplete);
function loaderComplete(e:Event):void
{
    // The output of the text file is available via the data property
    // of URLLoader.
    trace(loader.data);
}

For stuff on another domain, you will need to have a crossdomain file hosted to be able to load the text file.