how understand url not reachable (IOError) ? - actionscript-3

There are images inside of folder names like Slide1.jpg,Slide2.jpg, until Slide20.jpg. But program doesn't know how many images inside of folder. I dont want to count them also.(lets say total number of images = 100). But when press nextBtn until Slide20.jpg program will take:
Error #2044: Unhandled IOErrorEvent:. text=Error #2036: Load Never
Completed
When program Catches that error logically, it means there is no file name like Slide21.jpg. Which means total image number is 20. I want to go back when take at first time that error. Thanks for any advice
var totalImages=100;
var imageNumber=1;
var myLoader:Loader = new Loader();
//************LOADING IMAGES TO STAGE********************//
//elearningau.com/nondynamic/
var myRequest:URLRequest = new URLRequest("http://elearning.com/upandshow/Images/Slide"+imageNumber+".JPG");
myLoader.load(myRequest);
addChildAt(myLoader,1)
rightButton.addEventListener(MouseEvent.CLICK, nextImage);
function nextImage(event:MouseEvent){
if(imageNumber<totalImages)
{
imageNumber++;
} else {
imageNumber = 1;
}
reload();
}
function reload(){
removeChild(myLoader);
myRequest= new URLRequest("http://elearning.com/upandshow/Images/Slide"+imageNumber+".JPG");
myLoader.load(myRequest);
addChildAt(myLoader, 1);
}

You need to listen for errors:
myLoader.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
function ioErrorHandler(event:IOErrorEvent):void {
// this means there was an error - do whatever you want
}

Related

Animate 2021 Action Script 3 URLRequest causes Error 2035

I'm working on an application and we are using Flash to add an interface. During the initialize the app loads in a config file with references to image paths. When I use the Loader and URLRequest classes to load those images the path is not working and it's causing issues. Based on everything I've researched my code should work. I have a trace that outputs "source/icons/default.png" which is the correct relative path for the external image and loading the image works, but I need to use a variable to set the image path. Setting the URLRequest with a variable, new URLRequest("source/icons/" + default.png);, causes it use a full file path that looks wrong and reports a 2035 error. Is there something that I'm doing wrong? I'm not sure what to do at this point. I've verified the file exists and I've verified the path of the file.
Trace Ouput:
source/icons/default.png
Error Output:
[CompanionStatus] Error occurred while loading bitmap
[CompanionStatus] 2035 : Error #2035: URL Not Found. URL: file:///C|/Users/devtrooper/Desktop/project/source/icons/default.png
It looks like flash is replacing the colon (:) near the drive letter with a pipe (|) and I'm not sure if that is the issue.
Here is my method for loading images:
private function getBitmapData(nameString:String, imagePath:String):void{
try{
var bitmapDataFunction:Function = addBitmapDataToDictionary(nameString);
var path:String = ICON_FOLDER_PATH + imagePath;
var loader:Loader = new Loader();
var urlRequest:URLRequest = new URLRequest("source/icons/" + imagePath);
trace("URLRequest.url " + urlRequest.url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, bitmapDataFunction, false, 0, true);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, handleBitmapDataError, false, 0, true);
loader.load(urlRequest);
} catch (e:Error) {
log("Error occured while trying to load image data");
log(e.name + " : " + e.message);
}
}
you can use File method instead of urlRequest method like this:
var urlPath:String="";
var picFile:File = File.desktopDirectory.resolvePath("source/icons/default.png");//you can use applicationDirectory,applicationStorageDirectory,userDirectory,doumenntDirectory and ...
if (picFile.exists == true) {
urlPath = picFile.url;
}

I need to ping a website with ActionScript or Animate

I had found this code ( I need to ping to an network with flash or actionscript )
Im not sure i understand it or how to use it exactly - I need for this to redirect to another frame if it worked and another if it failed
Is anyone able to help?
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(HTTPStatusEvent.HTTP_STATUS, ldrStatus);
var url:String = "URL-TO-SITE";
var limit:int = 10;
var time_start:Number;
var time_stop:Number;
var times:int;
ping();
function ping():void
{
trace("pinging", url);
times = 0;
doThePing();
}
function doThePing():void
{
time_start = getTimer();
ldr.load(new URLRequest(url));
}
function ldrStatus(evt:*):void
{
if(evt.status == 200)
{
time_stop = getTimer();
trace("got response in", time_stop - time_start, "ms");
}
times++;
if(times < limit) doThePing();
}
Well it is trying to load a website 10 times and check the response. I would get rid of the limit though, it make no sense to try it 10 times in a row.
Don't forget that you will need a crossdomain.xml file on your server to be able to access it from flash.
You will need to add some more things for your purpose:
// add an event listener for a failed call
ldr.addEventListener(IOErrorEvent.IO_ERROR, ldrFailed);
ldr.addEventListener(SecurityErrorEvent.SECURITY_ERROR , ldrSecurityError);
function ldrFailed(evt:*):void
{
// loader failed (no internet connection), try to ping again
doFailedRedirect();
}
function ldrSecurityError(evt:*):void
{
// There is an internet connection but probably something is wrong with your crossdomain.xml
doFailedRedirect();
}
function doRedirect():void
{
// make your redirect here
}
function doFailedRedirect():void
{
// something went wrong, do your failed redirect here
}
Adjust the ldrStatus function:
function ldrStatus(evt:*):void
{
if(evt.status == 200)
{
// server responded with status 200 wich means everything is fine
time_stop = getTimer();
trace("got response in", time_stop - time_start, "ms");
doRedirect();
}
else
{
// there is an internet connection but the server returns something else (probably something is wrong with the server)
doFailedRedirect();
}
}

File.load() seemingly randomly crashing the program

My program has an export / import function that uses AS3Commons3-zip to make zipfiles of some content (pictures, small videos, small pdf's) inside the program. User selects an topic to make the zipfile out of, selects save location, program copies the files to tempDir (with File.createTempDirectory method) and then it loops through the tempDir with following code:
public function fileSelected(event:Event):void{
zipFile = event.currentTarget as File;
addFileToZip(tempDir, zip);
}
public function addFileToZip(file:File, zip:Zip, path:String=""):void{
if(file.isDirectory){
var directory:Array = file.getDirectoryListing();
filesToCompress = filesToCompress + directory.length;
dispatchEvent(new
for each (var f:File in directory){
addFileToZip(f, zip, path + "/" + file.name);
}
fileAddedToZip();
}else{
file.addEventListener(Event.COMPLETE, function():void{
var pathSplit:Array = file.nativePath.split(".tmp\\",2);
var fileNamePath:String = pathSplit[1] as String;
zip.addFile(fileNamePath, file.data);
fileAddedToZip();
});
file.addEventListener(ProgressEvent.PROGRESS, function(e:ProgressEvent):void{
totalLoaded = totalLoaded+e.bytesLoaded;
trace(totalLoaded+" <---TOTAL LOADED");
});
file.load();
}
}
public function fileAddedToZip():void{
filesCompressed++;
if (filesToCompress == filesCompressed){
var stream:FileStream = new FileStream();
stream.open(zipFile, FileMode.WRITE);
zip.serialize(stream);
stream.close();
tempDir.deleteDirectoryAsync(true);
}
}
And the code itself seems to work fine: the zip files import and export perfectly fine. Problem is that the program crashes at seemingly random times. Sometimes it goes through with 30 files, sometimes it crashes with 3. Sometimes it manages to make the same zip from same files 3 times in a row, then it crashes on the fourth try on some file and fifth try crashes on different file.
For example: Five tries with 35 copies of same image exported into the zip:
35/35, TOTAL LOADED trace: 3110337
35/35, TOTAL LOADED trace: 3110337
CRASH, TOTAL LOADED trace: 0 after initializing 26 files
35/35, TOTAL LOADED trace: 3110337
35/35, TOTAL LOADED trace: 3110337
CRASH, TOTAL LOADED trace: 0 after initializing 14 files
CRASH, crashed initializing first file
35/35, TOTAL LOADED trace: 3110337
35/35, TOTAL LOADED trace: 3110337
FREEZE WITH NO CRASH (stuck on file 17/35, cancel button works etc.), TOTAL LOADED trace: 1282932
I can see no pattern with it. File.load() method seems to be the culprit but no idea why. Crashes throw no errors at all. Only thing that is certain is that more the image sizes are in total (still under 100MB) the crashes are more common. Any ideas?
Solved this by using URLLoader instead:
var request:URLRequest = new URLRequest(file.nativePath);
var urlLoader:URLLoader = new URLLoader(request);
urlLoader.addEventListener(Event.COMPLETE, function (event:Event):void {
var pathSplit:Array = file.nativePath.split(".tmp\\",2);
var fileNamePath:String = pathSplit[1] as String;
zip.addFile(fileNamePath, event.target.data);
fileAddedToZip();
});
urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
urlLoader.load(request);

Adobe AIR : Worker can't load file

I have an issue with Workers in AIR : when I try to open a file in a non-primordial Worker, I get a Security error. When I try the same code in the primordial Worker, it works well.
I load another swf and pass its bytes when creating the second Worker.
First, I tried with URLLoader (code in 2nd worker) :
// Loading XML test
var loader:URLLoader = new URLLoader(new URLRequest('app:/config/generator/galaxy.xml'));
loader.addEventListener(Event.COMPLETE, function(evt:Event):void
{
// Trace
CEThreadDebugger.log(XML(loader.data).toString());
});
loader.addEventListener(IOErrorEvent.IO_ERROR, function(evt:IOErrorEvent):void
{
CEThreadDebugger.log(evt.text, CEThreadDebugger.ERROR);
});
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function(evt:SecurityErrorEvent):void
{
CEThreadDebugger.log(evt.text, CEThreadDebugger.ERROR);
});
I get this error :
[LOG]ERROR->Error #2048: Security sandbox violation: app:/SombresCieux.swf cannot load data from app:/config/generator/galaxy.xml.
Then I tried with File :
var file:File = File.applicationDirectory.resolvePath('config/generator/galaxy.xml');
var stream:FileStream = new FileStream();
stream.open(file, FileMode.READ);
var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
stream.close();
And I get this error (my code is in _mainToWorker method) :
[LOG]INFO->Error #0 : SecurityError -> file
SecurityError: file
at flash.filesystem::File$/initAppResourceDir()
at flash.filesystem::File$/get appResourceDirectoryPath()
at flash.filesystem::File$/get applicationDirectory()
at generator::Generator/_mainToWorker()
at engine.generic.system.concurrency::CEThreadMain/Evt_mainToWorker()
I noticed that the error comes from this line alone :
var file:File = File.applicationDirectory.resolvePath('config/generator/galaxy.xml');
So it works in the primordial Worker (main app), but not in the threads...
Can't the Workers access system's files nor load any file ? It's a quite huge limitation...
Thanks for the replies !
public function createWorker(swf:ByteArray,
giveAppPrivileges:Boolean = false):Worker
giveAppPrivileges:Boolean (default = false) — indicates whether the worker should be given application sandbox privileges in AIR. This parameter is ignored in Flash Player

Actionscript 3 Sound ioError Problem

i am having trouble with sound. i am loading it dynamically, url comes from flashvars.
App is working actually but stil gives error, unhandled ioError. but i already handled it.
`
var sound:Sound = new Sound()
try{
sound.load(new URLRequest(req));
} catch(e:IOError){
trace("catch ioerror");
}
sound.addEventListener(IOErrorEvent.IO_ERROR, function(evt:IOErrorEvent):void { trace("error:",evt) } );
sound.addEventListener(Event.COMPLETE, function(e:Event):void{
channel = sound.play(0,int.MAX_VALUE);
});`
Just to rule it out, try commenting out the line channel = sound.play(0,int.MAX_VALUE); perhaps it is this that is throwing the error and not the load, and you have no catch around this.
Try a neater approach in terms of code, might just have issues with your layout and such:
var request:URLRequest = new URLRequest(req);
sound.load(request);
sound.addEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.addEventListener(Event.COMPLETE, _complete);
function _ioError(e:IOErrorEvent):void
{
trace("File was not found");
_removeListeners();
}
function _complete(e:Event):void
{
channel = sound.play(0,int.MAX_VALUE);
_removeListeners();
}
function _removeListeners():void
{
sound.removeEventListener(IOErrorEvent.IO_ERROR, _ioError);
sound.removeEventListener(Event.COMPLETE, _complete);
}