DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Flex And PHP File Download
Here is the PHP.
<?php
/** ****************************************************************
* Working File Download XML Server Script for Flex
*
* MySnippets
* Free for use
*
* @author Jonnie Spratley
* @contact jonniespratley@gmail.com
******************************************************************* */
/* Set the content type for the browser */
header("Content-type: text/xml");
/* Establish a connection to database */
$link = mysql_connect('localhost', 'spratley_guest', 'guest') or die('Could not connect: ' . mysql_error());
/* Select the database */
mysql_select_db( 'spratley_tutorials', $link ) or die ( mysql_error() );
/* Build the query for the table */
$query = "SELECT * FROM flex_uploads ORDER BY file_date DESC";
/* Execute the query on the table */
$resultID = mysql_query( $query, $link ) or die( "Data not found." );
/* Set the header xml version */
$xml_output = "<?xml version=\"1.0\"?>\n";
/* Set the root node for the xml */
$xml_output .= "<files>\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<file>\n";
/* Set every node inside the child file node (id) */
$xml_output .= "\t\t<id>" . $row['file_id'] . "</id>\n";
/* file_name table column */
$xml_output .= "\t\t<name>" . $row['file_name'] . "</name>\n";
/* file_size table column */
$xml_output .= "\t\t<size>" . $row['file_size'] . "</size>\n";
/* file_type table column */
$xml_output .= "\t\t<type>" . $row['file_type'] . "</type>\n";
/* file_url table column */
$xml_output .= "\t\t<url>" . $row['file_url'] . "</url>\n";
/* file_date table column */
$xml_output .= "\t\t<date>" . $row['file_date'] . "</date>\n";
/* Close the child file now and print the child file node out */
$xml_output .= "\t</file>\n";
}/* Ends the loop */
/* Close the parent files node */
$xml_output .= "</files>";
/* Output all of the xml */
echo $xml_output;
?>
Here is the Flex.
<?xml version="1.0" encoding="utf-8"?>
<!--
/** *******************************************************************
* MySnippets
* Free for use
*
* @author Jonnie Spratley
* @contact jonniespratley@gmail.com
******************************************************************* */
-->
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%" creationComplete="init()"
xmlns:components="com.jonniespratley.advguidetoflex.view.chapter5.components.*">
<mx:Script>
<![CDATA[
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" );
}
]]>
</mx:Script>
<!--Download Button-->
<mx:Button label="Download"
click="downloadFile( lt_images.selectedItem.name )"
width="100%"/>
<!--Displays our Images-->
<mx:TileList id="lt_images" width="100%" height="100%">
<mx:itemRenderer>
<mx:Component>
<components:ImageRenderer/>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
<!--Refresh Button-->
<mx:Button label="Refresh"
click="getImages()"
width="100%"/>
</mx:VBox>





