quarta-feira, 17 de abril de 2013

File Upload


Como criar em Flex, um site de upload:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
backgroundColor="#ffffff">

<mx:Style source="styles/styles.css" />

<mx:HRule x="10" y="37" width="90%"/>
<mx:Text x="10" y="10" text="Uploading a File" styleName="headerStyle" id="label1"/>

<!-- 
THIS WILL NOT WORK UNLESS YOU ENTER THE CORRECT PATH TO THE PHP FILE ON YOUR SERVER
-->

<mx:Script>
<![CDATA[

// ENTER THE PATH TO THE FILE UPLOAD SCRIPT ON YOUR SERVER
// Check the bin-debug folder
// You may have to run clean on your project to get it to copy out the php files to the server
public var uploadFile:String = "http://localhost:8888/php/file_upload.php";

]]>
</mx:Script>

<mx:Button x="10" y="70" label="Upload" click="{upload()}"/>
<mx:Button x="83" y="70" label="Check if php file exists" click="{test()}"/>

<mx:Script>
<![CDATA[
import flash.net.navigateToURL;
import flash.events.DataEvent;

// we declare the file reference here so it is not destroyed by memory garbage collection
public var fileRef:FileReference = new FileReference();
// a class that is similar to a HTML form
public var request:URLRequest;

// opens a browser window for the user to select a file to upload
public function upload():void {
// listen for the upload events
// http://livedocs.adobe.com/flex/3/html/17_Networking_and_communications_7.html
fileRef.addEventListener(Event.SELECT, selectHandler);
fileRef.addEventListener(Event.OPEN, openHandler);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressHandler);
fileRef.addEventListener(Event.COMPLETE, completeHandler);
fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteHandler);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, httpSecurityErrorHandler);
fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpErrorHandler);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, httpIOErrorHandler);

// browse for the file to upload
// when user selects a file the select handler is called
try {
    var success:Boolean = fileRef.browse();
}
catch (error:Error) {
    trace("Unable to browse for files.");
       textarea1.text = "Unable to browse for files.";
}
}

// checks that the upload php file is where we think it is
public function test():void {
   request = new URLRequest(uploadFile);
navigateToURL(request,"_blank");
}

// when a file is selected we upload the file to the php file upload script on the server
public function selectHandler(event:Event):void {
   request = new URLRequest(uploadFile);
   try {
    // upload file
       fileRef.upload(request);
       textarea1.text = "Uploading " + fileRef.name + "...";
   }
   catch (error:Error) {
    // vague
       trace("Unable to upload file.");
       textarea1.text += "\nUnable to upload file.";
   }
}

// dispatched during file open. 
public function openHandler(event:Event):void {
   trace("File opened");
   textarea1.text += "\nFile opened";
}

// dispatched during file upload
public function progressHandler(event:ProgressEvent):void {
   trace("File upload in progress (" + event.bytesLoaded + " of " + event.bytesTotal + ")");
   textarea1.text += "\nFile upload in progress (" + event.bytesLoaded + " of " + event.bytesTotal + ")";
}

// dispatched when the file has been given to the server script
// this event does not receive a response from the server
// use DataEvent.UPLOAD_COMPLETE_DATA event as shown in uploadCompleteHandler
public function completeHandler(event:Event):void {
   trace("File uploaded");
   textarea1.text += "\nFile uploaded";
}

// dispatched when a file upload has completed
// this event can contain a response from the server as opposed to the Event.COMPLETE event
// the php upload file can send back information if we want it to
// the event.data and event.text properties would contain a response if any
public function uploadCompleteHandler(event:DataEvent):void {
   trace("Information about upload: \n" + String(event.text));
   textarea1.text += "\nInformation about upload \n" + event.text as String;
}

// dispatched when an http error occurs

// 404 is can't find file
// test the file exists
public function httpErrorHandler(event:HTTPStatusEvent):void {
   trace("HTTP error occured " + event.status);
   textarea1.text += "\nHTTP error occured - " + event.status;
}

// dispatched when an http io error occurs

// Error #2038: File I/O Error. - can't find the php file
// DO THE FOLLOWING:
// - check that the file name is spelled correctly in the uploadFile variable
// - check that the path to the file is correct (click test file exists button)
// - make sure you are running a php server locally (google search for MAMP or XAMPP)
// - make sure you are publishing to your php server
// - make sure you are pointing to the correct path in your local server (preferences > document root)
// - manually check the file is on your server
public function httpIOErrorHandler(event:IOErrorEvent):void {
   trace("HTTP IO error occured - " + event.text);
   textarea1.text += "\nHTTP IO error occured - " + event.text;
}

// dispatched when an http io error occurs

// Error #2049: Security sandbox violation means 
// your swf and php file are on different servers or directories
// make sure the php file is in the same or a subdirectory
// or add a cross domain security file to the same directory where the php file is
// check http://www.adobe.com/ for the latest documentation on cross domain policy files
public function httpSecurityErrorHandler(event:SecurityErrorEvent):void {
   trace("HTTP Security error occured - " + event.text);
   textarea1.text += "\nHTTP Security error occured - " + event.text;
}

]]>
</mx:Script>

<mx:TextArea x="10" y="100" width="90%" height="200" id="textarea1"/>

</mx:Application>

Flex Download


No Flex, como criar uma tela de download, no exemplo acima:


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" 
backgroundColor="#ffffff">

<mx:Style source="styles/styles.css" />

<mx:HRule x="10" y="37" width="90%"/>
<mx:Text x="10" y="10" text="Downloading a File" styleName="headerStyle" id="label1"/>

<mx:Button x="10" y="75" label="Download HTML Component" click="{download()}"/>

<mx:Script>
<![CDATA[
public function download():void {
// pass in url to file or php proxy
// create a new file reference instance
var request:URLRequest = new URLRequest("http://www.drumbeatinsight.com/examples/html/HTMLComponent1.0.0.zip");
var fileRef:FileReference = new FileReference();
fileRef.download(request);
}
]]>
</mx:Script>
</mx:Application>

segunda-feira, 15 de abril de 2013

FLEX x PROGRESS

O Blog tem por objetivo de orientar e tirar dúvidas sobre Flex ou Flash Builder que conversa com a base de dados Progress.

Eu estou com um exemplo do Flex que puxei no 4Each que lê a base de dados Progress. Esse foi um dos exemplos que usei para os meus desenvolvimentos através do EMS e Totvs 11:

Se alguém tiver exemplos melhores referente ao Flex que conversa com a Base de Dados Progress, sempre será bem vindo:

Código Fonte do Flex:



<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="Inicializar()">   

    <mx:AdvancedDataGrid id="DgPedidos" designViewDataType="tree" width="900" height="386" selectionMode="singleRow" x="{Application.application.width / 7}" y="67" alpha="0.5"  dataProvider="{companyHierarchy}">
        <mx:columns>
       <mx:AdvancedDataGridColumn headerText="Cliente / Pedido / Item" width="90" dataField="@cpi" textAlign="left" />
       <mx:AdvancedDataGridColumn headerText="Dt Pedido / Desc Item"   width="90" dataField="@dpi" textAlign="left" />
       <mx:AdvancedDataGridColumn headerText="Dt Entrega / Quantidade" width="90" dataField="@deq" textAlign="left" />
       <mx:AdvancedDataGridColumn headerText="Vendedor / Vl Unitário"  width="90" dataField="@vvl" textAlign="left" />
       <mx:AdvancedDataGridColumn headerText="Vl Pedido / Vl Total"    width="90" dataField="@vpt" textAlign="left" />
    </mx:columns>
    </mx:AdvancedDataGrid>

<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
         
          import mx.collections.HierarchicalData;

          [Bindable]
          private var companyHierarchy:HierarchicalData;
                    
   private function Inicializar():void{
    HttpBuscaPedidos.send();    
   }

       private function RetBuscaPedidos(event:ResultEvent):void{
        var companyData:XML  = new XML(event.result);
       
           companyHierarchy = new HierarchicalData(companyData.cliente);              
       }
        ]]>
    </mx:Script>
    <mx:HTTPService id="HttpBuscaPedidos"
    url="http://localhost/cgi-bin/cgiip.exe/WService=AdobeFlash/Projeto/Progress/customer.p"
       result="{RetBuscaPedidos(event)}"
       resultFormat="e4x"
       showBusyCursor="true"
    />
</mx:Application>

Fonte do Progress:


/*salvar como customer.p no diretório raiz*/

{src/web2/wrap-cgi.i}

output-content-type ("text/xml":U).

{&OUT} '<?xml version="1.0" encoding="utf-8"?>' Skip
       '<clientes>' Skip.

Define Variable d-val-total As Decimal No-undo.

For Each customer No-lock
    Break By customer.cust-num:

    If First-of(customer.cust-num) 
    Then Do:
       {&OUT} '<cliente cpi=~"' customer.Name '~">' skip.
    End.

    For Each order Of customer No-lock
        Break By order.order-num:
       
        If First-of(order.order-num)
        Then Do:
           {&OUT} '<pedido cpi=~"' order.order-num '~" dpi=~"' order.ship-date '~" deq=~"' order.promise-date '~">' Skip.
        End.

        For Each order-line Of order No-lock:
            Find First Item
                 Where Item.item-num = order-line.item-num
                 No-lock No-error.

            {&OUT} '<itens cpi=~"' order-line.item-num '~" dpi=~"' Item.item-name '~" deq=~"' order-line.qty '~" vvl=~"' order-line.price '~" vpt=~"' String(order-line.qty * order-line.price) '~"/>' Skip.
        End.

        If Last-of(order.order-num)
        Then Do:
           {&OUT} '</pedido>' Skip.
        End.                          


    End.


    If Last-of(customer.cust-num) 
    Then Do:
       {&OUT} '</cliente>' Skip.
    End.

End.

{&OUT} '</clientes>' Skip.