Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 9 of 9

Thread: Checking to see if a file exists before loading

  1. #1
    39
    posts
    Registered User

    Checking to see if a file exists before loading

    I was seaching for an easy way in AS3 to check if a file exists before loading it. There was not very much about so I wrote these classes to do it.

    There are two classes, the FileLoaderUtils and the UtilEvent. the FileLoaderUtils has the function doesFileExist(url:String) and when called the class uses the URLStream class to attempt to load the file. If the file exists a UtilEvent.FILE_EXISTS event is dispatched. If the URL is incorrect or the file doesn not exist a UtilEvent.FILE_NOT_FOUND event is dispatched.

    See the attached zip for the classes. Hope the may be of use to someone.

    Cheers
    Attached Files Attached Files
    Last edited by kipty; June 11th, 2009 at 12:08 PM.

  2. #2

    Thanks

    Quote Originally Posted by kipty View Post
    I was seaching for an easy way in AS3 to check if a file exists before loading it. There was not very much about so I wrote these classes to do it.

    There are two classes, the FileLoaderUtils and the UtilEvent. the FileLoaderUtils has the function doesFileExist(url:String) and when called the class uses the URLStream class to attempt to load the file. If the file exists a UtilEvent.FILE_EXISTS event is dispatched. If the URL is incorrect or the file doesn not exist a UtilEvent.FILE_NOT_FOUND event is dispatched.

    See the attached zip for the classes. Hope the may be of use to someone.

    Cheers
    Thanks buddy.

  3. #3
    hmm, why would i bother doing this? you can catch events when trying to load a file. you basically added a new step in loading a file instead loading it or not, from the first time.

  4. #4

    This helped me, thank you.

    Hey, nice one!

    As the last person implied, it is possible to use IOErrorEvent, when you load right off the bat (as is done in the Util class in question as well), but this helped me for the following reason: I'm currently trying out the also very handy CASA Lib classes (http://casalib.org/), which provide a number of programmer's shortcuts, including GroupLoad, which allows a simple process for loading multiple media items. However, they do not seem to provide error handling (I'll write them shortly so this was a nice and simple solution, and it kept me from having to modify CASA lib.

    If any readers want a more spelled out example beyond the initial instructions:

    var _loadUtils:FileLoaderUtils = new FileLoaderUtils();
    _loadUtils.addEventListener(UtilEvent.FILE_EXISTS, ifExistsFunction);
    _loadUtils.addEventListener(UtilEvent.FILE_NOT_FOU ND, ifNotFoundFunction);
    _loadUtils.doesFileExist(myImagePath);

    Anyway, thanks again.

  5. #5
    As an afterthough, there is a small downside to using this method in the GroupLoad setting. By initially checking to see if all files exist and then loading them, you end up starting a load and then cancelling, causing your Activity window to fill up with errors for each of the files you check. A better way would be to fully load the assets if they exist, and skip the rest along the way, for minimal error reports. Hope my headaches and trials/errors/poor planning helps others along the way.

    MAIN ANSWER
    Also it looks like Casa Lib does in fact do error checking, using IOErrorEvent in all Load classes that inherit properities from LoadItem. I should have looked a little harder haha.
    Last edited by menelec; August 10th, 2009 at 09:46 PM.

  6. #6
    39
    posts
    Registered User
    FYI

    I use this method because i just want to see if the file exists, i dont want to load the file at that exact time. I store which files exist and then i can configure my application depending on which files do exist. To be exact it is for a video player. The embed of the video player can contain many flashvars, like an image to display, caption xml files, audio description mp3 etc. I find out if these files exist before configuring the player. It was to make the implementation of the player into a CMS more simple.

    If there any better ways to do this let me know.

    Thanks for the comments menelec, its a good point about loading the file if they exist. I could adapt the class and store a ref to the loaded file in the class for access any time you want.

    Cheers

  7. #7
    I tried this class... doesnt seem to work maybe I'm doing something wrong

    no matter what URL i link to it seems to give me the file exists.

  8. #8
    It could be great to see a kind of "demo" aplying this classes

    Have a beer
    Miguel

  9. #9
    In the following example, I'm using the two classes mentioned in this forum post to detect if an image exists that is named to match the XML data file I am using for my example. For instance, Jeeper.jpg exists on my webserver, so the image is displayed when the Jeeper button is clicked. The image file for the Bubba button does NOT exist (Bubba.jpg), so by leveraging the FILE_NOT_FOUND event in the UtilEvent class, I simply display a generic image in lieu of a broken image display.

    http://labs.insideflex.com/flextrain...type/test.html

    These classes worked great as is, by the way. Here's a code snippet of how I used them:

    private var imgpath:String = "";

    public function displayData(sCeleb:String):void {
    imgpath = "images/" + sCeleb + ".jpg";

    var _loadUtils:FileLoaderUtils = new FileLoaderUtils();
    _loadUtils.addEventListener(UtilEvent.FILE_EXISTS, ifExistsFunction);
    _loadUtils.addEventListener(UtilEvent.FILE_NOT_FOU ND, ifNotFoundFunction);
    _loadUtils.doesFileExist(imgpath);
    }

    private function ifExistsFunction(uev:UtilEvent): void {
    imgCelebrity.source = imgpath;
    }

    private function ifNotFoundFunction(uev:UtilEvent): void {
    imgCelebrity.source = "images/sillyface.jpg";
    }
    Last edited by clarkmcg; September 12th, 2012 at 11:57 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012