The FileReference ClassThe FileReference class was created to allow users to browse for, upload, and download single files. It is important when using this class to import it first at the beginning of your script, like this: import flash.net.FileReferece; And to instantiate a new instance of the FileReference object, use this code as a template: var myFile_fr:FileReference = new FileReference(); PropertiescreationDateAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.creationData; Description: This Date property represents the creation date of the file on the end user's computer. creatorAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.creator; Description: This string property represents the Macintosh creator file type of the file on the end user's computer. modificationDateAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.modificationDate; Description: This Date property represents the last modification date of the file on the end user's computer. nameAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.name; Description: This string property is the name of the file on the end user's computer. Note that this will not return the path to the file, because that is a security risk. sizeAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.size; Description: This property represents the total size of the file in bytes. typeAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.type; Description: This string property represents the file type date of the file on the end user's computer. MethodsaddListenerAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.addListener(listener); Parameter:
Description: This method will add an event listener to an instance of the FileReference object. Example: This example will create a FileReference object and automatically call the browse method when you test it. After you select a file and click Open, all the properties of that file will be sent to the Output panel: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference 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); //browse file_fr.browse(); browseAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.browse(types); Parameter:
Description: This method will allow end users to browse for files on their local system and have them returned to Flash. Example: This example will create a FileReference object and automatically call the browse method when you test it. And it will limit the files to just a few image types. After you select a file and select Open, all the properties of that file will be sent to the Output panel:
cancelAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.cancel(); Description: This method will cancel any download or upload currently active. Example: This example will allow someone to download about 90% of the file and then cancel on them: import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //the listener var list_obj:Object = new Object(); list_obj.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void { if ((bytesLoaded/bytesTotal/2) >= .9) { //almost completed, but not quite file_fr.cancel(); } } //add the listener file_fr.addListener(list_obj); var path:String = "http://localHost.com/bigImage.jpg"; file_fr.download(path,"myImage.jpg"); downloadAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.download(path, fileName); Parameters:
Returns: Boolean If the window pops up allowing you to map where the file should be saved, it returns true; otherwise, false is returned. Description: This method will allow end users to download files from a server directly to their computer. Example: This example will allow someone to download a file from the local server: import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //the listener var list_obj:Object = new Object(); list_obj.onComplete = function(file:FileReference):Void { trace("ALL DONE!"); } //add the listener file_fr.addListener(list_obj); var path:String = "http://localHost.com/bigImage.jpg"; file_fr.download(path,"myImage.jpg"); removeListenerAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.removeListener(listener); Parameter:
Returns: Boolean If the listener is removed successfully, true is returned; otherwise, false is returned. Description: This method will remove an event listener from an instance of the FileReference object. Example: This example will create a FileReference object and automatically call the browse method when you test it. After you select a file and click Open, the name property will be sent to the Output panel, and the listener will be removed: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference 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(file_fr.removeListener(this)); } //add the listener to the FileReference object file_fr.addListener(list_obj); //browse file_fr.browse(); uploadAvailability: FP:8, AS:1.0 Generic Template: myFile_fr.upload(path); Parameter:
Returns: Boolean If there was no problem with calling this method (that is, no file in the FileReference yet), then TRue is returned; otherwise, false is returned. Description: This method will allow end users to upload files directly from Flash to a server. Example: This example will create a FileReference object and automatically call the browse method when you test it. After you have selected a file, it will call the upload method that will send the information to a server-side page. //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference events var list_obj:Object = new Object(); //the event for when a user selects a file and hits open list_obj.onSelect = function(){ file_fr.upload("http://localHost/upload.php"); } //add the listener to the FileReference object file_fr.addListener(list_obj); //browse file_fr.browse(); For a real working example, see Chapter 22, "PHP and Flash." EventsonCancelAvailability: FP:8, AS:1.0 Generic Template: Listener.onCancel = function(fileRef); Parameter:
Description: This event is triggered when the dialog window is open, and the user chooses Cancel. Example: This example will open a dialog box for a user to choose a file, and then it listens for the Cancel button: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference events var list_obj:Object = new Object(); //the event for when a user selects a file and hits cancel list_obj.onCancel = function(fileRef:FileReference){ trace("Cancel Hit"); } //add the listener to the FileReference object file_fr.addListener(list_obj); //browse file_fr.browse(); onCompleteAvailability: FP:8, AS:1.0 Generic Template: Listener.onComplete = function(fileRef); Parameter:
Description: This event is triggered when a file upload or download completes successfully. Example: This example will allow a user to upload a file to a PHP page; then it listens for when the upload is complete: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference events var list_obj:Object = new Object(); //the event for when a user selects a file and hits open list_obj.onCancel = function(fileRef:FileReference){ fileRef.upload("http://localHost/upload.php"); } //the event for when the file is finished uploading list_obj.onComplete = function(fileRef:FileReference){ trace("ALL DONE!!"); } //add the listener to the FileReference object file_fr.addListener(list_obj); //browse file_fr.browse(); For a real working example, see Chapter 22, "PHP and Flash." onHTTPErrorAvailability: FP:8, AS:1.0 Generic Template: Listener.onHTTPError = function(fileRef, error){ Parameter:
Description: This event is triggered when an HTTP error occurs during uploading. Exa-mple: This example will allow a user to upload a file to a PHP page; then it listens for an HTTP error: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference events var list_obj:Object = new Object(); //the event for when a user selects a file and hits open list_obj.onCancel = function(fileRef:FileReference){ fileRef.upload("http://localHost/upload.php"); } //the event for HTTP errors list_obj.onHTTPError = function(fileRef:FileReference, error:Number){ trace("HTTP error: with " + fileRef.name + ":error #" + error); } //the event for when the file is finished uploading list_obj.onComplete = function(fileRef:FileReference){ trace("ALL DONE!!"); } //add the listener to the FileReference object file_fr.addListener(list_obj); //browse file_fr.browse(); For a real working example, see Chapter 22, "PHP and Flash". onIOErrorAvailability: FP:8, AS:1.0 Generic Template: Listener.onIOError = function(fileRef){ Parameter:
Description: This event is triggered when any input or output errors occur. You will see these errors most often when servers require authentication, and because there are no means for this to be accomplished with Flash, an error will occur. Example: This example will allow a user to upload a file to a PHP page; then it listens for an IO error: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference events var list_obj:Object = new Object(); //the event for when a user selects a file and hits open list_obj.onCancel = function(fileRef:FileReference){ fileRef.upload("http://localHost/upload.php"); } //the event for IO errors list_obj.onIOError = function(fileRef:FileReference){ trace("Input/Output error occured"); } //the event for when the file is finished uploading list_obj.onComplete = function(fileRef:FileReference){ trace("ALL DONE!!"); } //add the listener to the FileReference object file_fr.addListener(list_obj); //browse file_fr.browse(); For a real working example, see Chapter 22, "PHP and Flash." onOpenAvailability: FP:8, AS:1.0 Generic Template: Listener.onOpen = function(fileRef){ Parameter:
Description: This event is triggered when an upload or download method is called. Example: This example will allow a user to upload a file to a PHP page; then it listens for when the upload starts: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference events var list_obj:Object = new Object(); //the event for when a user selects a file and hits open list_obj.onCancel = function(fileRef:FileReference){ fileRef.upload("http://localHost/upload.php"); } //the event for when the upload starts list_obj.onOpen = function(fileRef:FileReference){ trace("Upload has started"); } //the event for when the file is finished uploading list_obj.onComplete = function(fileRef:FileReference){ trace("ALL DONE!!"); } //add the listener to the FileReference object file_fr.addListener(list_obj); //browse file_fr.browse(); For a real working example, see Chapter 22, "PHP and Flash." onProgressAvailability: FP:8, AS:1.0 Generic Template: Listener.onProgress = function(fileRef, bytesLoaded, bytesTotal){ Parameter:
Description: This event is triggered periodically during either upload or download. Example: This example will download a file and trace the percentage downloaded as it progresses:
onSecurityErrorAvailability: FP:8, AS:1.0 Generic Template: Listener.onSecurityError = function(fileRef, error){ Parameter:
Description: This event is triggered when a security error occurs during either upload or download. Example: This example will attempt to download a file from the server, and if a security error occurs, it will be sent to the Output panel: import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //the listener var list_obj:Object = new Object(); //the event that is triggered when a security error occurs list_obj.onSecurityError = function(file:FileReference, error:String):Void { trace("Security error: " + error); } //add the listener file_fr.addListener(list_obj); var path:String = "http://localHost.com/bigImage.jpg"; file_fr.download(path,"myImage.jpg"); onSelectAvailability: FP:8, AS:1.0 Generic Template: Listener.onSelect = function(fileRef){ Parameter:
Description: This event is triggered when a user selects a file and either clicks Open or double-clicks the file. Example: This example will allow the user to select a file, and when the user clicks open, all the file properties will be sent to the Output panel: //import the FileReference Object import flash.net.FileReference; //the file reference object var file_fr:FileReference = new FileReference(); //object for listening to for FileReference 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); //browse file_fr.browse(); |