0 Replies Latest reply on Sep 22, 2008 2:04 PM by walker6o9

    Saving Files to User Hard Drive

    walker6o9 Level 1
      I'm using Adobe AIR to save files to the user's hard drive. The problem I'm having is that if the file is contained in folder and I try to save it to the User's harddrive, it creates that folder in the user's harddrive and saves the file in that folder. I've attached the code below, at least the parts of my code that I think are relevant. I can include the whole thing if need be.

      public function showDownloadScreen()
      {
      this.downloadscreens.enabled = true;
      this.downloadscreens.visible = true;
      this.downloadscreens.alpha = 100;

      this.downloadscreens.gotoAndStop("start");

      var files:String;
      var index:Number;
      var filename:String;
      var filepath:String;

      filepath = fileList[0];
      index = filepath.lastIndexOf("/");
      filename = filepath.substring( index + 1, filepath.length );
      files = filename;

      for (var i:Number = 1; i < fileList.length; i++)
      {
      filepath = fileList ;
      index = filepath.lastIndexOf("/");
      filename = filepath.substring( index + 1, filepath.length );

      files = files + ", " + filename;
      }

      this.downloadscreens.start.bodytext.text = "To save the selected file(s):\n" + files
      + "\n\nto your computer, please click the download button and select a destination.";

      }

      //--------------------------------------------------------------------------------
      public function showCompleteScreen():void
      {
      this.downloadscreens.enabled = true;
      this.downloadscreens.visible = true;
      this.downloadscreens.alpha = 100;

      this.downloadscreens.gotoAndStop("complete");

      var index:Number;
      var filepath:String;

      index = file.nativePath.lastIndexOf("/");
      filepath = file.nativePath.substring( index + 1, file.nativePath.length );

      this.downloadscreens.complete.bodytext.text = "Your file(s) have been saved to " + filepath + ".";
      }

      //--------------------------------------------------------------------------------
      public function showErrorScreen():void
      {
      this.downloadscreens.enabled = true;
      this.downloadscreens.visible = true;
      this.downloadscreens.alpha = 100;

      this.downloadscreens.gotoAndStop("complete");

      this.downloadscreens.complete.bodytext.text = "Error! File not found.";
      }

      //--------------------------------------------------------------------------------
      public function hideDownloadscreens(evt:Event):void
      {
      this.downloadscreens.alpha = 0;
      this.downloadscreens.enabled = false;
      this.downloadscreens.visible = false;

      this.downloadscreens.gotoAndStop("start");
      }

      //--------------------------------------------------------------------------------
      public function startDownload( evt:Event )
      {
      this.hideDownloadscreens(evt);

      file.addEventListener( Event.SELECT, dirSelected );
      file.browseForDirectory( "Select a directory" );
      }

      //--------------------------------------------------------------------------------
      // Open a directory selection window
      public function dirSelected( evt:Event )
      {
      if ( this.fileList.length > 0 )
      {
      this.currentFile = 0;

      this.loadFile();
      }
      }

      //--------------------------------------------------------------------------------
      public function loadFile():void
      {
      var filename:String = this.fileList[this.currentFile];
      var byteLoader:URLLoader = new URLLoader();

      byteLoader.dataFormat = URLLoaderDataFormat.BINARY;
      byteLoader.addEventListener( Event.COMPLETE, onDownloaded );
      byteLoader.addEventListener( SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler );
      byteLoader.addEventListener( HTTPStatusEvent.HTTP_STATUS, httpStatusHandler );
      byteLoader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );

      var fileRequest = new URLRequest( filename );

      try
      {
      byteLoader.load( fileRequest );
      }
      catch (error:Error)
      {
      this.showErrorScreen();
      }
      }

      //--------------------------------------------------------------------------------
      public function securityErrorHandler(event:SecurityErrorEvent):void
      {
      this.showErrorScreen();
      this.downloadscreens.complete.bodytext.text = "Error! The security settings prevent you from saving\nthese files to your computer.";
      }

      //--------------------------------------------------------------------------------
      public function httpStatusHandler(event:HTTPStatusEvent):void
      {
      this.showErrorScreen();
      this.downloadscreens.complete.bodytext.text = "Error! HTTP error, file wasn't found.";
      }

      //--------------------------------------------------------------------------------
      public function ioErrorHandler(event:IOErrorEvent):void
      {
      this.showErrorScreen();
      this.downloadscreens.complete.bodytext.text = "Error! IO error, file wasn't found.";
      }

      //--------------------------------------------------------------------------------
      // On file downloaded, save file
      public function onDownloaded(evt:Event)
      {
      var filename:String = this.fileList[this.currentFile];

      saveFile( file.nativePath + "/" + filename, evt );

      this.currentFile++;

      if ( this.currentFile < this.fileList.length )
      {
      loadFile();
      }
      else
      {
      showCompleteScreen();
      }
      }

      //--------------------------------------------------------------------------------
      // Write file to hard drive
      public function saveFile( filename:String, evt:Event )
      {

      var myFile:File = File.documentsDirectory.resolvePath( filename );
      var myFileStream:FileStream = new FileStream();

      myFileStream.open( myFile, FileMode.WRITE );
      myFileStream.writeBytes( evt.target.data, 0, evt.target.data );
      }