File Upload


One of the most exciting new features of Flash is the capability to download and upload files directly from your SWF. Flash accomplishes this by means of the FileReference object.

The FileReference object allows you to browse for files with the browse() method. You can even tell Flash what types of files you want the end user to be able to select. And when a user selects a file, the FileReference will have these available properties:

  • creationDate A Date object representing the creation date of the file on the users computer.

  • creator The Mac creator type of the file on the user's computer.

  • modificationDate A Date object representing the last modified date of the file.

  • name The name of the file.

  • size The size of the file in bytes.

  • type The file type of the file.

Following is an example that will allow you to select a file and then display all the information in the Output panel.

1.

Create a new Flash document.

2.

Drag an instance of the Button component onto the stage and give it an instance name of browse_butn and set the label to browse.

3.

Now create a second layer called actions and place the following code in the first frame of that layer:

 //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileRefernce events var list_obj:Object = new Object(); //the event for when a user selects a file and hits open list_obj.onSelect = function(){     trace("name: "+file_fr.name);     trace("creation date: "+file_fr.creationDate);     trace("modification date: "+file_fr.modificationDate);     trace("type: "+file_fr.type);     trace("size in bytes: "+file_fr.size);     trace("creator: "+file_fr.creator); } //add the listener to the FileReference object file_fr.addListener(list_obj); //event for the Button component browse_butn.clickHandler = function(){     file_fr.browse(); } 

This code does quite a few things. First, it imports the necessary class file for the FileReference object. Then it creates a generic object to use as a listener for the onSelect event that is triggered when a user selects a file and clicks Open. Then we create the actual event callback for the listener object. Next the listener object is added to our FileReference. And finally, we create the event for the Button component, so that when the button is clicked, the File Browse window will appear.

Test this movie, and when you select a file and click Open, you should see some information about the file in the Output panel, as shown in Figure 1.15.

Figure 1.15. One of the best new features in Flash is the capability to select files for upload and download.


If you want to see the FileReference object working with a server-side script to actually be able to upload images, check out Chapter 22, "PHP and Flash."




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net