Jonny
Jonny

Reputation: 16308

Check remote file existance with Flex 3

In Flex 3/AS 3, what would be a good way to check if a remote file exists? I'm thinking in PHP ways where you'd try to "fopen" a remote path (like "http://example.com/somefile.exe"), and see if it works or not. I'm not asking to just download all of the file, I just want to know if the file is there (and accessible).

Upvotes: 2

Views: 4017

Answers (3)

Eli Elad Elrom
Eli Elad Elrom

Reputation: 33

There is a utility class I developed that handle a check weather a file exists or not. Here's the code: https://github.com/eladelrom/eladlib/blob/master/EladLibFlex/src/com/elad/framework/utils/FileExistsUtil.as

And implementation looks like this:

var fileExists:FileExistsUtil = new FileExistsUtil();
fileExists.checkFile("file.jpg", 
function(eventType:String):void
{
 trace(eventType);
}, 
function(errorType:String, text:String):void
{
 trace(errorType+": "+text);
});

Upvotes: 1

Tom
Tom

Reputation: 12659

This is the best code I found for the job

var urlStream:URLStream = new URLStream();
urlStream.addEventListener(Event.OPEN, streamHandler);
urlStream.addEventListener(IOErrorEvent.IO_ERROR, streamHandler);
urlStream.load(new URLRequest("SOME_FILE"));

function streamHandler(e:Event):void {
    urlStream.close();
    if(e.type == Event.OPEN){
        trace("FILE EXISTS");
    } else if(e.type == IOErrorEvent.IO_ERROR){
        trace("FILE DOES NOT EXIST");
    }
}

Upvotes: 2

Will
Will

Reputation: 1189

You would probably need to attempt to load the file. If you get an IOError, the file doesn't exist (or your path is wrong). If it starts loading, by triggering a progress event then it exists. You can then cancel the remainder of the loading.

Alternatively you could try calling a PHP script from Flash which does what you have described, this could return a simple true/false.

Upvotes: 0

Related Questions