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
-
require_once 'Zend/Amf/Server.php';
-
require_once 'Zend/Amf/Exception.php';
-
require_once 'br/com/leonardofranca/vo/FileVO.php';
-
require_once 'br/com/leonardofranca/UploadZendAMF.php';
-
-
$server = new Zend_Amf_Server();
-
$server->setProduction(false);
-
-
$server->setClass('UploadZendAMF');
-
-
$server->setClassMap('FileVO',"br.com.leonardofranca.vo.FileVO");
-
-
echo($server->handle());
-
?>
Now our VO properties with the file name and binaries.
-
<?php
-
class FileVO
-
{
-
public $_explicitType = 'br.com.leonardofranca.vo.FileVO';
-
public $fileName;
-
public $fileData;
-
-
function __construct ()
-
{}
-
-
public function getFileName()
-
{
-
return $this->fileName;
-
}
-
-
public function setFileName($fileName)
-
{
-
$this->fileName = $fileName;
-
}
-
-
public function getFileData()
-
{
-
return $this->fileData;
-
}
-
-
public function setFileData($fileData)
-
{
-
$this->fileData = $fileData;
-
}
-
}
-
?>
Now our PHP class to be responsible for efetudar uploading.
-
<?php
-
class UploadZendAMF
-
{
-
public function __construct()
-
{
-
-
}
-
-
public function upload(FileVO $data)
-
{
-
try
-
{
-
$fileData = $data->getFileData();
-
return true;
-
}
-
catch (Exception $e)
-
{
-
throw new Exception($e->getMessage());
-
}
-
}
-
}
-
?>
Now let the view layer using Adobe Flex, we start with our VO.
-
package br.com.leonardofranca.vo
-
{
-
-
[Bindable]
-
[RemoteClass(alias="br.com.leonardofranca.vo.FileVO")
-
public class FileVO
-
{
-
-
public function FileVO()
-
{
-
}
-
-
{
-
return _fileName;
-
}
-
-
{
-
_fileName = value;
-
}
-
-
{
-
return _fileData;
-
}
-
-
{
-
_fileData = value;
-
}
-
-
}
-
}
Now our mxml that will load the bytes from the file to send to the Zend AMF.
-
<?xml version="1.0" encoding="utf-8"?>
-
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
-
xmlns:s="library://ns.adobe.com/flex/spark"
-
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
-
<fx:Declarations>
-
<!-- Place non-visual elements (e.g., services, value objects) here -->
-
</fx:Declarations>
-
<fx:Script>
-
<![CDATA[
-
import br.com.leonardofranca.vo.FileVO;
-
-
import mx.controls.Alert;
-
import mx.events.FlexEvent;
-
import mx.rpc.events.FaultEvent;
-
import mx.rpc.events.ResultEvent;
-
import mx.rpc.remoting.mxml.RemoteObject;
-
import mx.utils.ObjectUtil;
-
-
private var ro:RemoteObject;
-
private var fileRef:FileReference;
-
private var fileTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg)", "*.jpg; *.jpeg");
-
private var allTypes:Array = new Array(fileTypes);
-
-
-
protected function application1_creationCompleteHandler(event:FlexEvent):void
-
{
-
ro = new RemoteObject();
-
ro.destination = "nao faz diferença nenhuma usando com Zend AMF";
-
ro.endpoint = "http://localhost:81/ZendAmf/teste_upload.php";
-
ro.source = "br.com.leonardofranca.UploadZendAMF";
-
ro.addEventListener(ResultEvent.RESULT, handlerResult);
-
ro.addEventListener(FaultEvent.FAULT, handlerFault);
-
-
btnProcurar.addEventListener(MouseEvent.CLICK, handlerUpload);
-
btnSend.addEventListener(MouseEvent.CLICK, uploadVideos);
-
}
-
-
protected function handlerUpload(evt:MouseEvent=null):void
-
{
-
fileRef = new FileReference();
-
fileRef.addEventListener(Event.SELECT,selectHandler);
-
// fileRef.addEventListener(Event.COMPLETE,completeHandler);
-
// fileRef.addEventListener(ProgressEvent.PROGRESS,progressHandler);
-
fileRef.browse(allTypes);
-
}
-
-
protected function selectHandler(evt:Event):void
-
{
-
txtFile.text = fileRef.name;
-
fileRef.load();
-
}
-
-
protected function uploadVideos(evt:MouseEvent=null):void
-
{
-
var data:ByteArray = new ByteArray();
-
fileRef.data.readBytes(data,0,fileRef.data.length);
-
-
var vo:FileVO = new FileVO();
-
vo.fileName = fileRef.name;
-
vo.fileData = data;
-
-
ro.upload(vo);
-
}
-
-
protected function handlerResult(re:ResultEvent):void
-
{
-
trace(ObjectUtil.toString(re.message.body));
-
if(Boolean(re.message.body))
-
{
-
Alert.show("Arquivo enviado com sucesso!","Sucesso!");
-
}
-
else
-
{
-
Alert.show("Não foi possivel enviar o arquivo!","Error!");
-
}
-
}
-
-
protected function handlerFault(fault:FaultEvent):void
-
{
-
Alert.show(fault.fault.faultString,"Error!");
-
}
-
-
]]>
-
</fx:Script>
-
<mx:Form>
-
<mx:FormItem label="Envio de arquivos" direction="horizontal">
-
<s:TextInput id="txtFile"/>
-
<s:Button id="btnProcurar" label="Procurar"/>
-
<s:Button id="btnSend" label="Enviar"/>
-
</mx:FormItem>
-
</mx:Form>
-
</s:Application>
Buy the source code $2.99
[paid-downloads id="3"]
More source and PDFs click here

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!
Sir Leonardo, thank you for making our research easier, with this tutorial File uploads with Adobe Flex and Zend AMF very Informative…