File uploads with Adobe Flex and Zend AMF

Zend AMF is an implementation done in PHP to work with the communication protocol binary AMF (Action Message Format) and is part of ZendFramework. I had to implement a system to upload files that were a little different than what is typically used in Flash, with this feature had to be integrated into the Zend AMF.
Researching a little on the net, found a solution that was simpler than I thought based on that article with a few adjustments.
Begin with our gateway to be used as endpoint in Adobe Flex.

PHP:
  1. <?php
  2.       require_once 'Zend/Amf/Server.php';
  3.       require_once 'Zend/Amf/Exception.php';
  4.       require_once 'br/com/leonardofranca/vo/FileVO.php';
  5.       require_once 'br/com/leonardofranca/UploadZendAMF.php';
  6.        
  7.       $server = new Zend_Amf_Server();
  8.       $server->setProduction(false);
  9.        
  10.       $server->setClass('UploadZendAMF');
  11.        
  12.       $server->setClassMap('FileVO',"br.com.leonardofranca.vo.FileVO");
  13.    
  14.       echo($server->handle());
  15.       ?>

Now our VO properties with the file name and binaries.

PHP:
  1. <?php
  2.       class FileVO
  3.       {
  4.           public $_explicitType = 'br.com.leonardofranca.vo.FileVO';
  5.           public $fileName;
  6.           public $fileData;
  7.          
  8.           function __construct ()
  9.           {}
  10.          
  11.           public function getFileName()
  12.           {
  13.               return $this->fileName;
  14.           }
  15.        
  16.           public function setFileName($fileName)
  17.           {
  18.               $this->fileName = $fileName;
  19.           }
  20.        
  21.           public function getFileData()
  22.           {
  23.               return $this->fileData;
  24.           }
  25.        
  26.           public function setFileData($fileData)
  27.           {
  28.               $this->fileData = $fileData;
  29.           }
  30.       }
  31.       ?>

Now our PHP class to be responsible for efetudar uploading.

PHP:
  1. <?php
  2.       class UploadZendAMF  
  3.       {
  4.           public function __construct()
  5.           {
  6.            
  7.           }
  8.          
  9.           public function upload(FileVO $data)
  10.           {
  11.               try
  12.               {
  13.                   $fileData = $data->getFileData();
  14.                   file_put_contents( 'C:\\apache\\htdocs\\images\\' . $data->getFileName(), $fileData);//your dir
  15.                   return true;  
  16.               }
  17.               catch (Exception $e)
  18.               {
  19.                   throw new Exception($e->getMessage());
  20.               }
  21.           }
  22.       }
  23.       ?>

Now let the view layer using Adobe Flex, we start with our VO.

ACTIONSCRIPT3:
  1. package br.com.leonardofranca.vo
  2.       {
  3.           import flash.utils.ByteArray;
  4.        
  5.           [Bindable]
  6.           [RemoteClass(alias="br.com.leonardofranca.vo.FileVO")
  7.           public class FileVO
  8.           {
  9.               private var _fileName:String;
  10.               private var _fileData:ByteArray;
  11.              
  12.               public function FileVO()
  13.               {
  14.               }
  15.        
  16.               public function get fileName():String
  17.               {
  18.                   return _fileName;
  19.               }
  20.        
  21.               public function set fileName(value:String):void
  22.               {
  23.                   _fileName = value;
  24.               }
  25.        
  26.               public function get fileData():ByteArray
  27.               {
  28.                   return _fileData;
  29.               }
  30.        
  31.               public function set fileData(value:ByteArray):void
  32.               {
  33.                   _fileData = value;
  34.               }
  35.        
  36.           }
  37.       }

Now our mxml that will load the bytes from the file to send to the Zend AMF.

MXML:
  1. <?xml version="1.0" encoding="utf-8"?>
  2.       <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3.                     xmlns:s="library://ns.adobe.com/flex/spark"
  4.                     xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
  5.           <fx:Declarations>
  6.               <!-- Place non-visual elements (e.g., services, value objects) here -->
  7.           </fx:Declarations>
  8.           <fx:Script>
  9.               <![CDATA[
  10.                  import br.com.leonardofranca.vo.FileVO;
  11.                
  12.                  import mx.controls.Alert;
  13.                  import mx.events.FlexEvent;
  14.                  import mx.rpc.events.FaultEvent;
  15.                  import mx.rpc.events.ResultEvent;
  16.                  import mx.rpc.remoting.mxml.RemoteObject;
  17.                  import mx.utils.ObjectUtil;
  18.                
  19.                  private var ro:RemoteObject;
  20.                  private var fileRef:FileReference;
  21.                  private var fileTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg)", "*.jpg; *.jpeg");
  22.                  private var allTypes:Array = new Array(fileTypes);
  23.      
  24.                
  25.                  protected function application1_creationCompleteHandler(event:FlexEvent):void
  26.                  {
  27.                      ro = new RemoteObject();
  28.                      ro.destination = "nao faz diferença nenhuma usando com Zend AMF";
  29.                      ro.endpoint = "http://localhost:81/ZendAmf/teste_upload.php";
  30.                      ro.source = "br.com.leonardofranca.UploadZendAMF";
  31.                      ro.addEventListener(ResultEvent.RESULT, handlerResult);
  32.                      ro.addEventListener(FaultEvent.FAULT, handlerFault);
  33.                    
  34.                      btnProcurar.addEventListener(MouseEvent.CLICK, handlerUpload);
  35.                      btnSend.addEventListener(MouseEvent.CLICK, uploadVideos);
  36.                  }
  37.                
  38.                  protected function handlerUpload(evt:MouseEvent=null):void
  39.                  {
  40.                      fileRef = new FileReference();
  41.                      fileRef.addEventListener(Event.SELECT,selectHandler);
  42.                      //          fileRef.addEventListener(Event.COMPLETE,completeHandler);
  43.                      //          fileRef.addEventListener(ProgressEvent.PROGRESS,progressHandler);
  44.                      fileRef.browse(allTypes);
  45.                  }
  46.                
  47.                  protected function selectHandler(evt:Event):void
  48.                  {
  49.                      txtFile.text = fileRef.name;
  50.                      fileRef.load();
  51.                  }
  52.                
  53.                  protected function uploadVideos(evt:MouseEvent=null):void
  54.                  {
  55.                      var data:ByteArray = new ByteArray();
  56.                      fileRef.data.readBytes(data,0,fileRef.data.length);
  57.                    
  58.                      var vo:FileVO = new FileVO();
  59.                      vo.fileName = fileRef.name;
  60.                      vo.fileData = data;
  61.                    
  62.                      ro.upload(vo);
  63.                  }
  64.                
  65.                  protected function handlerResult(re:ResultEvent):void
  66.                  {
  67.                      trace(ObjectUtil.toString(re.message.body));
  68.                      if(Boolean(re.message.body))
  69.                      {
  70.                          Alert.show("Arquivo enviado com sucesso!","Sucesso!");
  71.                      }
  72.                      else
  73.                      {
  74.                          Alert.show("Não foi possivel enviar o arquivo!","Error!");
  75.                      }
  76.                  }
  77.                
  78.                  protected function handlerFault(fault:FaultEvent):void
  79.                  {
  80.                      Alert.show(fault.fault.faultString,"Error!");
  81.                  }
  82.                
  83.              ]]>
  84.           </fx:Script>
  85.           <mx:Form>
  86.               <mx:FormItem label="Envio de arquivos" direction="horizontal">
  87.                   <s:TextInput id="txtFile"/>
  88.                   <s:Button id="btnProcurar" label="Procurar"/>
  89.                   <s:Button id="btnSend" label="Enviar"/>
  90.               </mx:FormItem>
  91.           </mx:Form>
  92.       </s:Application>

Buy the source code $2.99
[paid-downloads id="3"]

More source and PDFs click here

Related Articles

  • No Related Post
Was this article helpful? feel free to make a donation and help keep the blog in the air
ActionScript 3.0, Flex, PHP, ZendAMF

2 comments


  1. Allen

    Hi Leonardo,greate tutorial thanks.But how about the Flash IDE Version,can you point out me?Just via zendAMF and Flash(!Flex && !Flash Builder) to create upload images.Thanks!

  2. Sir Leonardo, thank you for making our research easier, with this tutorial File uploads with Adobe Flex and Zend AMF very Informative…

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>