2 Replies Latest reply on Oct 11, 2006 7:41 PM by tamchsg

    Download/Upload file

    tamchsg Level 1
      I am looking for the class files com/example/programmingas3/fileio/FileDownload.as and com/example/programmingas3/fileio/FileUpload.as.

      Actually I am looking for an example to extract data to excel, word, or just as simple text file. (should also work with openoffice).
      No Java like http://jakarta.apache.org/poi/index.html.
      No clipboard like http://mannu.livejournal.com/348299.html
      No Microsoft OWC like http://webddj.sys-con.com/read/273886.htm should work on firefox/safari as well.
        • 1. Re: Download/Upload file
          tamchsg Level 1
          I find a solution to download files, actually two:
          -----------------------MX file ------------------------------
          <?xml version="1.0"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" xmlns:t1="actionscript.*"
          verticalAlign="middle" creationComplete="initb();">
          <mx:Panel title="Download File" paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10">
          <mx:ProgressBar id="downloadProgress" label="" mode="manual" />
          <mx:ControlBar horizontalAlign="right">
          <mx:Button id="startDownload" label="Download..." click="inita();" />
          <mx:Button id="startDownload2" label="Download2..." click="fileDownload2.startDownload();" />
          <mx:Button id="cancelDownload" label="Cancel" click="fileDownload2.cancelDownload();" enabled="false" />

          </mx:ControlBar>
          </mx:Panel>

          <mx:Script>
          <![CDATA[
          import actionscript.FileReference_download;
          import actionscript.FileDownload;
          public var fileDownload:FileReference_download;
          public var fileDownload2:FileDownload=new FileDownload();
          public function inita():void {
          fileDownload =new FileReference_download();
          }
          public function initb():void {
          fileDownload2.init(downloadProgress,cancelDownload);
          }

          ]]>
          </mx:Script>

          </mx:Application>
          -----------------------------AS FILES-----------------------------
          FILE 1
          package actionscript
          {

          import flash.events.ContextMenuEvent;
          import flash.ui.ContextMenu;
          import flash.ui.ContextMenuItem;
          import mx.core.Application;
          import mx.controls.Label;
          import flash.events.Event;
          import flash.events.EventDispatcher;
          import mx.core.ComponentDescriptor;
          import mx.core.UIComponent;
          import mx.containers.Canvas;
          import mx.controls.Button;
          import mx.controls.ProgressBar;
          import flash.events.ProgressEvent;
          import flash.net.FileReference;
          import flash.net.URLRequest;
          public class FileDownload
          {
          public function FileDownload():void{

          }
          /**
          * Hard-code the URL of file to download to user's computer.
          */
          private const DOWNLOAD_URL:String = " http://localhost/xxx.txt";
          private var filename:String="xxx.txt";
          /**
          * Create a FileReference instance to handle the file download.
          */
          private var fr:FileReference;

          /**
          * Define reference to the download ProgressBar component.
          */
          private var pb:ProgressBar;

          /**
          * Define reference to the "Cancel" button which will immediately stop
          * the current download in progress.
          */
          private var btn:Button;
          /**
          * Begin downloading the file specified in the DOWNLOAD_URL constant.
          */
          public function startDownload():void {
          var request:URLRequest = new URLRequest();
          request.url = DOWNLOAD_URL;
          fr.download(request,filename);
          }



          /**
          * Set references to the components, and add listeners for the OPEN,
          * PROGRESS, and COMPLETE events.
          */
          public function init(pb:ProgressBar, btn:Button):void {
          /*
          Set up the references to the progress bar and cancel button, which are
          passed from the calling script.
          */
          this.pb = pb;
          this.btn = btn;

          fr = new FileReference();
          fr.addEventListener(Event.OPEN, openHandler);
          fr.addEventListener(ProgressEvent.PROGRESS, progressHandler);
          fr.addEventListener(Event.COMPLETE, completeHandler);

          }


          /**
          * When the OPEN event has dispatched, change the progress bar's label
          * and enable the "Cancel" button, which allows the user to abort the
          * download operation.
          */
          private function openHandler(event:Event):void {
          pb.label = "DOWNLOADING %3%%";
          btn.enabled = true;
          }

          /**
          * While the file is downloading, update the progress bar's status.
          */
          private function progressHandler(event:ProgressEvent):void {
          pb.setProgress(event.bytesLoaded, event.bytesTotal);
          }

          /**
          * Once the download has completed, change the progress bar's label one
          * last time and disable the "Cancel" button since the download is
          * already completed.
          */
          private function completeHandler(event:Event):void {
          pb.label = "DOWNLOAD COMPLETE";
          btn.enabled = false;
          }

          /**
          * Cancel the current file download.
          */
          public function cancelDownload():void {
          fr.cancel();
          pb.label = "DOWNLOAD CANCELLED";
          btn.enabled = false;
          }
          }


          }
          ---------------------------File 2------------------------------------
          package actionscript
          {
          import flash.display.Sprite;
          import flash.events.*;
          import flash.net.FileReference;
          import flash.net.URLRequest;
          import flash.net.FileFilter;

          public class FileReference_download extends Sprite {
          private var downloadURL:URLRequest;
          private var fileName:String = "hello.txt";
          private var file:FileReference;

          public function FileReference_download() {
          downloadURL = new URLRequest();
          downloadURL.url = " http://localhost/hello.txt";
          file = new FileReference();
          configureListeners(file);
          file.download(downloadURL, fileName);
          }

          private function configureListeners(dispatcher:IEventDispatcher):void {
          dispatcher.addEventListener(Event.CANCEL, cancelHandler);
          dispatcher.addEventListener(Event.COMPLETE, completeHandler);
          dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
          dispatcher.addEventListener(Event.OPEN, openHandler);
          dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
          dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
          dispatcher.addEventListener(Event.SELECT, selectHandler);
          }

          private function cancelHandler(event:Event):void {
          trace("cancelHandler: " + event);
          }

          private function completeHandler(event:Event):void {
          trace("completeHandler: " + event);
          }

          private function ioErrorHandler(event:IOErrorEvent):void {
          trace("ioErrorHandler: " + event);
          }

          private function openHandler(event:Event):void {
          trace("openHandler: " + event);
          }

          private function progressHandler(event:ProgressEvent):void {
          var file:FileReference = FileReference(event.target);
          trace("progressHandler name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
          }

          private function securityErrorHandler(event:SecurityErrorEvent):void {
          trace("securityErrorHandler: " + event);
          }

          private function selectHandler(event:Event):void {
          var file:FileReference = FileReference(event.target);
          trace("selectHandler: name=" + file.name + " URL=" + downloadURL.url);
          }
          }

          }
          • 2. Re: Download/Upload file
            tamchsg Level 1
            After the download was find i tried the upload, but i got am error.
            HTTPStatusEvent: [HTTPStatusEvent type="httpStatus" bubbles=false cancelable=false eventPhase=2 status=405]
            ioErrorHandler: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2038: File I/O Error.

            Could anybody help