PDA

View Full Version : Calling an event in application from .as class file


vistasoul
07-17-2006, 05:47 PM
Hello,

I have an .as Class file that have a few event handlers in it.... I'm using this class to upload files to my server... here is the code.


package as3
{
import flash.display.Sprite;
import flash.events.*;
import flash.net.FileReference;
import flash.net.URLRequest;

public class FileUpload extends Sprite {
private var uploadURL:URLRequest;
private var file:FileReference;

public function FileUpload() {
uploadURL = new URLRequest();
uploadURL.url = "http://www.mywebsite.com/upload.php";
file = new FileReference();
file.addEventListener(Event.SELECT, selectHandler);
file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
}

public function FileBrowse():void
{
file.browse();
}

private function selectHandler(event:Event):void {
var file:FileReference = FileReference(event.target);
file.upload(uploadURL);
}

private function ioErrorHandler(event:IOErrorEvent):void {
trace("ioErrorHandler: " + event);
}

private function progressHandler(event:ProgressEvent):void {
var file:FileReference = FileReference(event.target);
trace("progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}

private function completeHandler(event:Event):void {
trace("completeHandler: " + event);
}
}
}


^^ How can I fire an event in my main application using one of those event handlers?

For example.. I'm trying to change the currentState of my appliction using the selectHandler above.

in my main application all I have to do is call "this.currentState="state"; and it works... but I don't know how to do this from within my class file.

Thank you.

hangalot
07-19-2006, 09:31 AM
you need to add a listener to your class and dispatch an event you listen to somewhere else

DangerAhead
02-02-2007, 10:24 PM
if you run into the Error #2038. AND you're using FireFox you might want to read this important thread:

http://tech.groups.yahoo.com/group/flexcoders/message/55890

it says Firefox has problems handing the FileReference over to the browser. I can't seem to get FileReference to work in FireFox 1.5.