Lomadee, uma nova espécie na web. A maior plataforma de afiliados da América Latina



Luiz Henrique de Angeli

luizdeangeli@gmail.com


Componente para Upload com Barra de Progresso

segunda-feira, 3 de agosto de 2009


Fiz um componente para fazer Upload de Arquivo com Barra de Progresso.
Abaixo os arquivos para fazer os testes.

FileUpload.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:TitleWindow
 xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="horizontal"
 title="Selecionar Arquivo"
 showCloseButton="true"
 close="PopUpManager.removePopUp(this);"
 creationComplete="init()">
 
 <mx:NumberFormatter id="numberFormatter" rounding="up"/>
 
 <mx:Form width="100%" height="100%" enabled="{fileUrl.toString()!=''}">
  <mx:FormItem direction="horizontal" width="100%">
   <mx:TextInput id="txt_filename" editable="false" width="160"/>
   <mx:Button id="btn_Localizar" click="localizar();" label="Localizar" icon="@Embed(source='../img/16x16/ico_lupa+.png')" useHandCursor="true" buttonMode="true"/>
   <mx:Button id="btn_enviar" click="enviar();" label="Enviar" icon="@Embed(source='../img/16x16/ico_confirma.png')" useHandCursor="true" buttonMode="true"/>
  </mx:FormItem>  
  <mx:FormItem direction="horizontal" width="100%" visible="{uploadProgresso}" includeInLayout="{uploadProgresso}">
   <mx:ProgressBar id="pgbProgresso" mode="manual" labelPlacement="center" width="100%" label="CARREGANDO {numberFormatter.format(pgbProgresso.percentComplete)}%"/>
  </mx:FormItem>
 </mx:Form>
 
<mx:Script>
 <![CDATA[
  import mx.managers.PopUpManager;
  import mx.controls.Alert;
  
 
  [Bindable] public var fileRef   : FileReference;
  [Bindable] private var uploadProgresso  : Boolean = false;
  [Bindable] private var uploadPorcento  : int = 0;
  
  [Bindable] public var fileUrl    : String;
  [Bindable] public var fileName   : String = "arquivo";  
  [Bindable] public var fileType   : String = "*.*";
  [Bindable] public var fileFilter   : String = "Arquivo";
  [Bindable] public var fileCloseComplete: Boolean = true;
  
  private function init():void
  {
   fileRef = new FileReference();
   fileRef.addEventListener(Event.SELECT, onSelect);
   fileRef.addEventListener(Event.COMPLETE, onUploadComplete);   
   fileRef.addEventListener(ProgressEvent.PROGRESS, progresso);
   fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fimUpload);   
  }
  
  public function localizar():void
  { 
   var tipos:FileFilter = new FileFilter(fileFilter,fileType);
   var tiposArray:Array = new Array(tipos);
   this.fileRef.browse(tiposArray);
  }
  
  
  public function fimUpload(e:DataEvent):void
  {
   uploadProgresso = false
  }
  
  private function progresso(e:ProgressEvent):void
  {
   pgbProgresso.setProgress(e.bytesLoaded,e.bytesTotal);   
   uploadProgresso = true;
  }
  
  private function erro(e:Event):void{
  
  }
 
  private function onSelect(event:Event):void {
   this.txt_filename.text = fileRef.name;
  }
 
  private function onUploadComplete(event:Event):void
  {
   uploadProgresso = false;
   if(fileCloseComplete)
    PopUpManager.removePopUp(this);
  }
 
  public function enviar():void
  {
   if (fileRef == null) return;
  
   var request:URLRequest = new URLRequest(fileUrl); 
   request.method = URLRequestMethod.POST; 
   fileRef.upload(request, fileName);
  }
 
 ]]>
 </mx:Script>
</mx:TitleWindow>




Usando.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
 <mx:Script>
  <![CDATA[
   import mx.utils.StringUtil;
   import mx.controls.Alert;
   import mx.managers.PopUpManager;   
   
   private function adicionarArquivo():void
{
 var fileUpload : FileUpload;
 fileUpload = new FileUpload();
 
 fileUpload.fileUrl = "http://www.urldoupload.com.br/upload.php";
 fileUpload.fileType = "*.pdf";
 fileUpload.fileFilter = "Arquivo PDF";
 
 
 PopUpManager.addPopUp(fileUpload,this,true);
 PopUpManager.centerPopUp(fileUpload);
 
 fileUpload.fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, fimUpload);
}
   
   public function fimUpload(e:DataEvent):void
   {    
    if(StringUtil.trim(e.data.toString())=="ERRO")
    {
     Alert.show('Erro ao salvar arquivo!','Atenção!')
    }else{
     Alert.show(e.data,'Atenção!')
     
    }    
   }
   
  ]]>
 </mx:Script>
 <mx:Button x="110" y="50" label="Arquivo" click="adicionarArquivo()"/>
</mx:Application>


upload.php

<?php
 $arquivo = $_FILES["arquivo"] ? $_FILES["arquivo"] : false;

 $retorno="ERRO";
 
 if($arquivo)
 {
  $url = "./pastadosarquivos/";
  
  if(@move_uploaded_file($arquivo["tmp_name"],$url."".basename($arquivo["tmp_name"]))){
   $retorno = basename($arquivo["tmp_name"]); 
  }
 } 
 
 echo $retorno;
 
?>

0 comentários:


Submarino.com.br
Submarino.com.br