Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Flex And PHP File Download

DZone's Guide to

Flex And PHP File Download

·
Free Resource
Here is the PHP.



  \n";

/* Set the root node for the xml */
$xml_output .= "
  
   \n";

/* Loop through all records in the query and output */
for( $x = 0 ; $x < mysql_num_rows( $resultID ) ; $x++ )
{
	/* Result rows */
    $row = mysql_fetch_assoc( $resultID );
    
	/* Set the child node */
    $xml_output .= "\t
   
    \n";
    
	/* Set every node inside the child file node (id) */
    $xml_output .= "\t\t
    
     " . $row['file_id'] . "
    \n";
    
    /* file_name table column */
    $xml_output .= "\t\t
    
     " . $row['file_name'] . "
    \n";
    
    /* file_size table column */
	$xml_output .= "\t\t
    
     " . $row['file_size'] . "
    \n";
	
	/* file_type table column */
	$xml_output .= "\t\t
    
     " . $row['file_type'] . "
    \n";
	
	/* file_url table column */
	$xml_output .= "\t\t
    
     " . $row['file_url'] . "
    \n";
	
	/* file_date table column */
	$xml_output .= "\t\t
    
     " . $row['file_date'] . "
    \n";
    
    /* Close the child file now and print the child file node out */
    $xml_output .= "\t
   \n";
    
}/* Ends the loop */

/* Close the parent files node */
$xml_output .= "
  ";

/* Output all of the xml */
echo $xml_output; 

?>



Here is the Flex.



  

  

  
	
   
    
		
			import mx.rpc.http.HTTPService;
			import mx.rpc.events.FaultEvent;
			import mx.events.FlexEvent;	
			import mx.rpc.events.ResultEvent;
			import flash.events.*;
			import flash.net.*;		
			import mx.controls.Alert;
			import mx.events.*;
			import mx.managers.*;			
			
			/* Our File reference */
			private var fileRef:FileReference;
			
			/* Our image service that is goin to connect to get the xml */
			private var svcImages:HTTPService;
			
			/* Our XML url */
			private var filexmlURL:String = "php/flex_file_download.php";
			
			/* Create a new httpservice and file reference, then register some even listeners, then get the images */				
			private function init():void
			{
				//For security purposes but it seems only to work when using air
				Security.allowDomain( "*" );
				Security.loadPolicyFile( "crossdomain.xml" );
					
				svcImages = new HTTPService();
				fileRef = new FileReference();				
				svcImages.addEventListener( FaultEvent.FAULT, faultHandler );
				svcImages.addEventListener( ResultEvent.RESULT, resultHandler );
				getImages();
			}			
			
			/* Makes the call to the xml script */
			private function getImages():void 
			{
				svcImages.url = filexmlURL;
				svcImages.method = URLRequestMethod.GET;	
				svcImages.send(); 				
			}	
						
			/* Handles any results that happen */
			private function resultHandler( event:ResultEvent ):void 
			{
				lt_images.dataProvider = event.target.lastResult.files.file;
			}				
			
			/* Downloads the selected file to users computer */
			private function downloadFile( whatFile:String ):void
			{			
				var request:URLRequest = new URLRequest();
					request.method = URLRequestMethod.GET;					
					request.url = "http://advguidetoflex.jonniespratley.com/php/uploads/" + whatFile;					
			
					fileRef.download( request );			
			}
	
			/* Handles any faults that happen */			
			private function faultHandler( event:FaultEvent ):void 
			{
				Alert.show( "There was a error fetching the images.", "Error" );
			}
		
	
   
	
	
   
	
   
		
	
   
	
   
		
    
			
     
				
      
			
     
		
    		
	
   	
	
	
   
	
   

  
Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}