Skip to content


Downloading Multiple Files with Flex 3 and AMFPHP

I originally was thinking that I would be able to do this by having the user select the files of their choice and click one button to download the items. So depending on their selection the Flex app would download the first item, then upon complete download of the first item it would download the second item and so on.. Unfortunately I could not get this to work on various browsers as the dialog box to download the file location would never appear on the second file. So with a little exploring I was able to accomplish the task of downloading multiple files of the users choosing with the use of:

Here is a list of things that I needed to get this working.

  1. AMFPHP – Lee Brimelow has an excellent tutorial to get you started: http://www.gotoandlearn.com/play?id=78
  2. PHP Class file to create zip documents from an array. You can find the one I used here: http://www.phpconcept.net/pclzip/index.en.php (note I am using PHP 5.*)
  3. I also grabbed this class file from FlexExamples.com for using a checkbox control as a list item renderer

Here is the process:

  1. So first the user will select the files he/she wishes to download
  2. The user will need to click a button to notify the server the array of files and generate a unique .zip document
  3. The download button will need to be active once the zip file is created so the user can then download the new .zip file
  4. User clicks the download button to download the file
  5. Will need to run a cron to delete all the generated .zip files as to not load up my server. (I won’t cover this step)

Honestly I am not a PHP programmer, but this is the Service Class that I put in my Service folder in my AMFPHP setup…

The file name is ZipFile.php (also don’t forget to inlude the library for zipping files)

<?php

class ZipFile
{
	/**
	* Demo ZipFile
	* @returns file name of zip object
	*/

	function ZipFileTest($fileArr) // this is what gets called from flash
	{
		// include the library from http://www.phpconcept.net/pclzip/index.en.php
		include_once("pclzip.lib.php");

		// create a unique file name.. this is prepended with manewc.com-
		$uniqueFileName = uniqid("manewc.com-");

		// create the zip
		$archive = new PclZip('../relative/path/to/put/this/zip/file'.$uniqueFileName.'.zip');

		$v_list = $archive->create($fileArr);

  		if ($v_list == 0) {
    		$status = "Error : ".$archive->errorInfo(true);
  		}
  		else
  		{
  			$status = $uniqueFileName;
  		}

		return $status;

	}
}

?$gt;

In Flex, I just need to add the ListItemValueObject.as file from FlexExamples.com. I added this file in src/com/flexexamples/.

Here is my Main.mxml file to create the ui:

Please note that my editor generally does not render my flex code properly.. check this file here: Main.mxml.txt




    
        
    
    

    
    
    

    
        
        
        
    

    

    
        
            
                
                    
                        
                            
                    
                
            
        
        
        
            
        
    

    


I’ll have to admit that this code needs to be cleaned up.. just ran out of time at the moment.

Posted in AMFPHP, Actionscript 3, Flex Web Development.


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. mark says

    sweet.
    and lee has some great stuff.

  2. handoyo says

    Thanks for the example…Nice work…



Some HTML is OK

or, reply to this post via trackback.