Results 1 to 9 of 9
-
June 11th, 2009, 12:06 PM #139Registered User
postsChecking 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.
CheersLast edited by kipty; June 11th, 2009 at 12:08 PM.
-
July 31st, 2009, 03:29 PM #2
-
July 31st, 2009, 05:15 PM #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.
-
August 10th, 2009, 03:09 PM #42Registered User
postsThis 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.
-
August 10th, 2009, 09:36 PM #52Registered User
postsAs 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.
-
October 5th, 2009, 05:28 AM #639Registered User
postsFYI
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
-
October 21st, 2009, 02:35 PM #710Registered User
postsI 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.
-
February 16th, 2012, 05:25 PM #8
It could be great to see a kind of "demo" aplying this classes
Have a beerMiguel
-
September 12th, 2012, 11:48 PM #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.
Helping the world see your business

Reply With Quote



Bookmarks