Tuesday, August 3, 2010

Working with files in Actionscript 3: Part 2

Today we're going to learn about the moment user is interracting with the download dialog window.

There are two events possible in this situation - select and cancel. To find out when user pressed the button, we listen to the select event.

fileReference.addEventListener(Event.SELECT, onSelect);

When event select has occured, we can see what name has user chosen by reading the name property of FileReference object:

function onSelect(event:Event){
trace("User selected file and gave it a name: " + event.target.name);
}

However user can also click the cancel button. If he does, the dialog window closes and file doesn't download. We can also find out when Cancel is clicked:

fileReference.addEventListener(Event.CANCEL, onCancel);

function onCancel(Event){
trace("User didn't want to download the file");
}

It is very important to only continue after we know that the user has selected a file, because download() method itself doesn't halt the Actionscript code we have.

Thank you for reading this tutorial!

Related:

Working with files in Actionscript 3: Part 1
Working with files in Actionscript 3: Part 3
Working with files in Actionscript 3: Part 4
Working with files in Actionscript 3: Part 5

2 comments:

Anonymous said...

im using this tutorial in flash cs3. Do you have to import anything? im getting this error

1046: Type was not found or was not a compile-time constant: Event.

I tried importing the event class and the file reference class. Just wondering what im missing?

Kirill Poletaev said...

Well, that error means that the event class is not imported... make sure you include this in your code:

import flash.events.Event

Post a Comment