2 Replies Latest reply on Sep 12, 2007 5:57 AM by GaryThompson76

    FileReference - Carn't Download after upload

    GaryThompson76
      Can anyone help

      I have an application that interfaces a repository, the upload works fine and the download works fine,
      but as soon as you upload a file you cannot download, Does the FileReference need resetting or is
      there something i'm not clearing out ?

      Any Help Would be great !

      // ----------------------------------------------------------- ActionScript file
      import flash.net.FileReference;
      import mx.utils.Base64Encoder;

      import flash.net.URLRequest;
      import flash.net.URLRequestHeader;
      import flash.net.URLRequestMethod;
      import flash.net.URLVariables;

      private var fileRef:FileReference;


      [Bindable] public var uploadURL:String = " http://myserver.uk/servlet/parserupload";
      [Bindable] public var tempDir:String = "/var/tmp/";
      [Bindable] public var AppStatus:String = "null";

      [Bindable] public var DepositfileUrlLocation:String;
      [Bindable] public var DepositfileName:String;
      [Bindable] public var DepositfilemimeType:String;
      [Bindable] public var DepositPID:String;
      [Bindable] public var DepositType:String;

      [Bindable] public var DownloadVersionDateTime:String = "null";
      [Bindable] public var DownloadVersionfileLocation:String = "null";
      //public var credentials:String;

      // -----------------------------------------------------------------------------
      // ------------------------------------------------ File Upload / Download SetUp
      private function initApp():void {

      fileRef = new FileReference();
      fileRef.addEventListener(Event.CANCEL, traceEvent);
      fileRef.addEventListener(Event.COMPLETE, completeEvent);
      fileRef.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, onUploadCompleteData);
      fileRef.addEventListener(Event.SELECT, selectEvent);
      fileRef.addEventListener(IOErrorEvent.IO_ERROR, traceEvent);
      fileRef.addEventListener(Event.OPEN, traceEvent);
      fileRef.addEventListener(ProgressEvent.PROGRESS, progressEvent);
      fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, traceEvent);

      }

      private function traceEvent(event:Event):void {
      var tmp:String = "================================\n";
      AppLog.text += tmp + event.type + " event:" + mx.utils.ObjectUtil.toString(event) + "\n" ;

      //Upload.visible=false;
      //MainPanel.enabled=true;
      }

      private function ioErrorEvent(event:IOErrorEvent):void{
      Alert.show("IOError:" + event.text);
      traceEvent(event);

      //Upload.visible=false;
      //MainPanel.enabled=true;
      }

      private function selectEvent(event:Event):void{

      AppLog.text ="sELECT eVENT " + "\r" + AppLog.text;
      AppLog.text ="App Status - " + AppStatus + "\r" + AppLog.text;

      if (AppStatus == "DownloadFile"){

      traceEvent(event);

      MainPanel.enabled=false;
      Upload.visible=true;
      progressBar.label = "Downloading...";

      } else {
      upload_button.enabled = true;
      FileToUpload.text = fileRef.name;
      }

      progressBar.setProgress(0, 100);
      progressBar.label = "Progress 0%";
      }

      private function progressEvent(event:ProgressEvent):void {
      progressBar.setProgress(event.bytesLoaded, event.bytesTotal);
      traceEvent(event);
      }

      private function completeEvent(event:Event):void {
      progressBar.label = "Complete.";
      FileToUpload.text = "";
      traceEvent(event);

      AppLog.text ="Upload Complete " + "\r" + AppLog.text;

      Upload.visible=false;
      MainPanel.enabled=true;
      AppStatus == "null"

      }
      // -----------------------------------------------------------------------------
      // -------------------------------------------------------- File Upload Complete
      private function onUploadCompleteData( e : DataEvent ) : void{

      AppLog.text ="Bollocks Bollocks Bollocks" + "\r" + AppLog.text;

      var data:XML = new XML(e.data);

      AppLog.text ="filename " + data[0].file.fileName + "\r" + AppLog.text;
      AppLog.text ="contentType " + data[0].file.contentType + "\r" + AppLog.text;
      AppStatus == "null"

      DepositfileName = data[0].file.fileName;
      DepositfileUrlLocation = data[0].file.fileUrlLocation;
      DepositPID = Mypath[Mypath.length-1].PID;
      DepositType = data[0].file.contentType;

      AppLog.text ="DepositfileName " + DepositfileName + "\r" + AppLog.text;
      AppLog.text ="DepositfileUrlLocation " + DepositfileUrlLocation + "\r" + AppLog.text;
      AppLog.text ="DepositPID " + DepositPID + "\r" + AppLog.text;

      RepoWorkFlowServiceForDeposit.deposit.send();
      }
      // -----------------------------------------------------------------------------
      // ----------------------------------------------------------------- File Upload
      private function LookToUpload():void {
      AppStatus = "UploadFile";
      fileRef.browse()
      }
      private function uploadFile(endpoint:String):void {
      AppStatus = "UploadFile";

      Upload.title="File Upload";
      //MainPanel.enabled=false;

      var params:URLVariables = new URLVariables();

      params.userID = userID;

      params.ftpServerIpAddress = ftpIP;
      params.ftpDirectory = ftpDirectory;
      params.urlFilePath = UrlFilePath;
      params.tempDirectory = tempDir;
      params.ftpServerPort = ftpPort;

      var req:URLRequest = new URLRequest(endpoint);
      req.method = URLRequestMethod.POST;

      req.data = params;

      fileRef.upload(req, "custom");

      Upload.visible=true;
      progressBar.label = "Uploading...";

      }
      // -----------------------------------------------------------------------------
      // --------------------------------------------------------------- File Download
      public function startDownload():void {
      AppStatus = "DownloadFile";
      Upload.title="File Download";


      AppLog.text = "Download File " + listbox.selectedItem.Title + "\r" + AppLog.text;


      var path:String = " http://myserver.co.uk/servlet/fileDownload?URL=http://" + fedoraIP + ":" + fedoraPort + "/fedora/get/" + listbox.selectedItem.PID + "/file&filename=" + listbox.selectedItem.Title;

      AppLog.text = "path " + path + "\r" + AppLog.text;

      var request:URLRequest = new URLRequest();
      request.url = path;

      var fileName:String = listbox.selectedItem.Title;
      fileRef.download(request, fileName);
      }
        • 1. FileReference - Carn't Download after upload
          levancho Level 3
          in reality there is no remote fileRefference Object,
          Each FileReference object refers to a single file on the user's disk and has properties that contain information about that file.
          NOTES from SDK :

          When the file is downloaded successfully, with call myFileRefference.downloadrequest, fileName); only the name property is populated when the select event is dispatched; other properties of the FileReference object are populated when complete event is dispatched if the download is successful.

          which means that filerefference (always) at the end will reference properties of local file not remote.
          also its notable :
          ---
          The FileReference.upload() and FileReference.download() functions are nonblocking. These functions return after they are called, before the file transmission is complete. In addition, if the FileReference object goes out of scope, any upload or download that is not yet completed on that object is canceled upon leaving the scope. Be sure that your FileReference object remains in scope for as long as the upload or download is expected to continue.
          ----

          There might be better way but , I would use two FileRefference Objects to solve the problem (plus with two fleRefference you can have asynchronous upload/downloads going on) .




          • 2. Re: FileReference - Carn't Download after upload
            GaryThompson76 Level 1
            Thanks Very Much levancho !!

            I implemented a second FileReference Object, so one handles the uploads and the other the downloads. Works a treat.

            Carn't see the Forest for the Tree's !