# How to check if image file exist before loading it into UIoader

**URL:** <https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339>\
**Category:** General\
**Tags:** flash, template\
**Created:** [October 14, 2019, 1:05pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339 "2019-10-14T13:05:35Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![nerijus](https://casparcgforum.org/user_avatar/casparcgforum.org/nerijus/32/778_2.png) [@nerijus](https://casparcgforum.org/u/nerijus)\
**Post date:** [October 14, 2019, 1:05pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/1 "2019-10-14T13:05:35Z")

</div>

Hello, is there a way to check if Image file exists before loading it into UIloader with Actionscript (Adobe Animate)?

The thing is that I need to display image and if that image does not exist, the whole templates does not play. I’ve looked through AS3 classes that check if file exists (file.exists), but nothing seems to work. Is it even possible to do this using only Actionscript? Or am I missing something?

I am using the following code to display image and just want a solution for a situation when Image does not exist.

```auto
package
{
	import flash.display.MovieClip;
	import se.svt.caspar.ICommunicationManager;
	import se.svt.caspar.IRegisteredDataSharer;
	import se.svt.caspar.template.CasparTemplate;
      
	public class Intro extends CasparTemplate
	{
		private const customParameterDescription:XML = 	
		<parameters>
	   		<parameter id="logo1" type="string" info="File URL" /> 		
		</parameters>;
		
		override public function SetData(xmlData:XML):void 
		{			
		   for each (var element:XML in xmlData.children())
		   {
			  if (element.@id == "logo1") 		
			  {
				  logo1.source = element.data.@value.toString();
			  }
		   }
		   super.SetData(xmlData);		
		}
	}
}

```

---

<div class="post-metadata">

**Author:** ![dangb9](https://casparcgforum.org/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@dangb9](https://casparcgforum.org/u/dangb9)\
**Post date:** [October 14, 2019, 1:49pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/2 "2019-10-14T13:49:53Z")

</div>

```auto
var tmp_file:File = File.documentsDirectory.resolvePath('my_file.txt');

if (tmp_file.exists) {
    // File exists
} else {
    // File doesn't exist
}

```

---

<div class="post-metadata">

**Author:** ![nerijus](https://casparcgforum.org/user_avatar/casparcgforum.org/nerijus/32/778_2.png) [@nerijus](https://casparcgforum.org/u/nerijus)\
**Post date:** [October 14, 2019, 3:43pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/3 "2019-10-14T15:43:20Z")

</div>

I used your code, but it didn’t run after I imported File class. I found that I have to switch to AIR runtime and that worked, it generated the template, but nothing is shown when I play the template due to errors. I suppose it’s because of AIR runtime.

I added the code so that it runs and does nothing just to check if it works and does not interfere, but it seems it does interfere.

> package  
> {  
> import flash.display.MovieClip;  
> import se.svt.caspar.ICommunicationManager;  
> import se.svt.caspar.IRegisteredDataSharer;  
> import se.svt.caspar.template.CasparTemplate;
> 
> import flash.filesystem.File;
> 
> public class Third extends CasparTemplate  
> {  
> private const customParameterDescription:XML =   
>   
>   
> ;
> 
> ```
> override public function SetData(xmlData:XML):void 
> {			
> for each (var element:XML in xmlData.children())
> {
> if (element.@id == "logo") 		
> {
>   			  
> var tmp_file:File = File.documentsDirectory.resolvePath('my_file.txt');
>   			
> if (tmp_file.exists) {
> // File exists
> } else {
> // File doesn't exist
> }
>   			
> Logo.source = element.data.@value.toString();					
> }
>   	 
> }
> super.SetData(xmlData);		
> }
> 
> ```
> 
> }  
> }

 ![image](https://casparcgforum.org/uploads/default/original/1X/ecb3071a3fc2583c6ae27936359d4731013ea843.png)

This is what CasparCG writes after playing the template:

> [2019-10-14 18:22:39.245] [8480] [error] flash-player[cg20.fth.1080p2500|1920x1080] [error] @Add@1@VerifyError: Error #1014@  
> [2019-10-14 18:22:39.258] [8480] [error] flash-player[cg20.fth.1080p2500|1920x1080] [error] @Play@1@ReferenceError: No template queued on layer 1@

Any help?

---

<div class="post-metadata">

**Author:** ![didikunz](https://casparcgforum.org/user_avatar/casparcgforum.org/didikunz/32/7_2.png) [@didikunz](https://casparcgforum.org/u/didikunz)\
**Post date:** [October 14, 2019, 4:25pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/4 "2019-10-14T16:25:18Z")

</div>

I simly use a try catch block. See [here](https://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ed1.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7ec5) for details. You can set a boolean variable in the catch block to know in your code, if the image has loaded or not.

---

<div class="post-metadata">

**Author:** ![Andrea](https://casparcgforum.org/user_avatar/casparcgforum.org/andrea/32/775_2.png) [@Andrea](https://casparcgforum.org/u/Andrea)\
**Post date:** [October 15, 2019, 11:07am UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/5 "2019-10-15T11:07:08Z")

</div>

Hi,  
I have the same issue, I’m working for live streaming motocross races and have al lot of images to load, like 100~200 riders photos

I’m using the same command : `image.source="image_path"`

I tried the try~catch method without any luck  
as far I understand flash player just ignore the absence of the file without errors and ignore the catch block

but in casparcg the absence of the file generate the removeTemplate command

`[2019-10-15 10:41:21.618] [1336] [error] flash-player[cg20.fth.576p2500|1024x576] [error] <invoke name="OnError" returntype="xml"><arguments><string>@Runtime error on template &quot;file://C:\Program Files\CasparCG Server 2.0.7\templates_RAM\\TESTER.ft@1@Error #2036@</string></arguments></invoke>`

`[2019-10-15 10:41:21.643] [1336] [debug] flash-player[cg20.fth.576p2500|1024x576] [activity] <invoke name="OnActivity" returntype="xml"><arguments><string>Command finished @RemoveTemplate@1@</string></arguments></invoke>`

There is any way to tell CasaparCG Server to ignore this kind of error?  
[like the flash player standalone]

thanks

---

<div class="post-metadata">

**Author:** ![didikunz](https://casparcgforum.org/user_avatar/casparcgforum.org/didikunz/32/7_2.png) [@didikunz](https://casparcgforum.org/u/didikunz)\
**Post date:** [October 15, 2019, 11:09am UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/6 "2019-10-15T11:09:40Z")

</div>

But if it ignores the try catch, then something is wrong with that part of the code.

> [@Andrea](#):
>
> I’m using the same command : `image.source="image_path"`

What is `"image_path"`? If it should be a variable name, it should not have the quotation marks.

Try this line: `try{Image.source = image_path;} catch(error:Error){}`  
That works for me.

---

<div class="post-metadata">

**Author:** ![Andrea](https://casparcgforum.org/user_avatar/casparcgforum.org/andrea/32/775_2.png) [@Andrea](https://casparcgforum.org/u/Andrea)\
**Post date:** [October 15, 2019, 11:54am UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/7 "2019-10-15T11:54:05Z")

</div>

works for you (didikunz) either in caspar server or flash standalone player?

yes, the path is without quotation marks  
actually is edited from xml data [real time from time keepers]  
like this:  
`Rider1.FaceSource.source = "Images/Face/" + Current_xml.results.result[pilot_position].@additional8 + "_" + Current_xml.results.result[pilot_position].@no + "_" + Current_xml.results.result[pilot_position].@lastname + ".png";`

works fine if the file exists, doesn’t run at all if the file doesn’t exist

I tried :

```auto
                try
                {
                	Rider2.xfirstname.text = " try block "; 
                	Rider2.FaceSource.source = "test_image_1.jpg";
                }
                catch (err:Error) 
                {
                	Rider2.xlastname.text = " catch block "; 
                }

```

borrowing the text fields for debug purposes  
but if I remove the test\_image\_1.jpg the “catch block” text doesn’t show up

---

<div class="post-metadata">

**Author:** ![rrebuffo](https://casparcgforum.org/user_avatar/casparcgforum.org/rrebuffo/32/165_2.png) [@rrebuffo](https://casparcgforum.org/u/rrebuffo)\
**Post date:** [October 15, 2019, 12:10pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/8 "2019-10-15T12:10:03Z")

</div>

I’d just use GreenSock ImageLoader class. It handles errors and scaling way better than UILoader.

---

<div class="post-metadata">

**Author:** ![didikunz](https://casparcgforum.org/user_avatar/casparcgforum.org/didikunz/32/7_2.png) [@didikunz](https://casparcgforum.org/u/didikunz)\
**Post date:** [October 15, 2019, 1:22pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/9 "2019-10-15T13:22:48Z")

</div>

> [@Andrea](#):
>
> Rider2.FaceSource.source = “test\_image\_1.jpg”;

The filename needs to be a fully qualified file URL. So the file `D:\Bilder\Sarah_24.JPG` should be given as `file:///D:/Bilder/Sarah_24.JPG` otherwise it will not work.

> [@Andrea](#):
>
> but if I remove the test\_image\_1.jpg the “catch block” text doesn’t show up

Because no error is risen, when you remove that block.

---

<div class="post-metadata">

**Author:** ![nerijus](https://casparcgforum.org/user_avatar/casparcgforum.org/nerijus/32/778_2.png) [@nerijus](https://casparcgforum.org/u/nerijus)\
**Post date:** [October 16, 2019, 7:05am UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/10 "2019-10-16T07:05:51Z")

</div>

Exactly the same problem as Andrea has. It seems that the filepath is passed into the casparCG despite the Try-catch block. So when the Try part is initiated, it doesn’t matter what the results are, because the path is loaded either way ant the whole template crashes. Does it work for you didi, how?

I also tested it with Server 2.0.7 and 2.1.0. Same problem for me.

Template works when file exists.

> try {  
> title1.text = " Try block ";  
> Logo.source = “file:///D:/-CasparCG Server/templates/DELA.PNG”;
> 
> } catch (err:Error) {  
> title2.text = " Error ";  
> }

I assume we need to check if file exists before assigning it to UIloader. Any ideas?

---

<div class="post-metadata">

**Author:** ![didikunz](https://casparcgforum.org/user_avatar/casparcgforum.org/didikunz/32/7_2.png) [@didikunz](https://casparcgforum.org/u/didikunz)\
**Post date:** [October 16, 2019, 9:41am UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/11 "2019-10-16T09:41:52Z")

</div>

> [@nerijus](#):
>
> So when the Try part is initiated, it doesn’t matter what the results are, because the path is loaded either way ant the whole template crashes.

You are right, I tested it, and found the same, as you. Sorry for saying something wrong. The code needs to be:

```auto
package
{
	import flash.display.MovieClip;
	import flash.events.IOErrorEvent;
	import se.svt.caspar.template.CasparTemplate;

	public class UILoader_Test extends CasparTemplate
	{
		private function loaderIOErrorHandler(errorEvent:IOErrorEvent):void
		{
			title2.text = " Error ";
		}
		
		override public function postInitialize():void
		{
			Logo.addEventListener(IOErrorEvent.IO_ERROR, loaderIOErrorHandler);
			
			title1.text = " Try to load ";
			Logo.source = "file:///D:/-CasparCG Server/templates/DELA.PNG";
		}

		override public function Play():void
		{
		}

		override public function Stop():void
		{
			removeTemplate();
		}

		override public function SetData(xmlData:XML):void 
		{		
			//super.SetData(xmlData);		
		}
	}
}

```

Because the ioError happens asynchronous, we need to catch the event. But you could leave the `loaderIOErrorHandler` function empty, to just prevent the template from crashing. By the way: the code, that loads the file into the `UILoader` can also be somewhere else (like in the `SetData()` function). Because the `addEventListener` is called inside `postInitialize()`, that is called before anything else, the code would still work.

You find a zip file with my test template [here](https://www.dropbox.com/s/s2kdmvrwl8s7sd8/UILoader_Test.zip?dl=0).

---

<div class="post-metadata">

**Author:** ![nerijus](https://casparcgforum.org/user_avatar/casparcgforum.org/nerijus/32/778_2.png) [@nerijus](https://casparcgforum.org/u/nerijus)\
**Post date:** [October 16, 2019, 11:37am UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/12 "2019-10-16T11:37:31Z")

</div>

Thank you! It works. Now everything runs beautifully!

---

<div class="post-metadata">

**Author:** ![Andrea](https://casparcgforum.org/user_avatar/casparcgforum.org/andrea/32/775_2.png) [@Andrea](https://casparcgforum.org/u/Andrea)\
**Post date:** [October 20, 2019, 9:12pm UTC](https://casparcgforum.org/t/how-to-check-if-image-file-exist-before-loading-it-into-uioader/2339/13 "2019-10-20T21:12:56Z")

</div>

Thank you very much didikunz, works fine  
fantastic!
