2 Replies Latest reply on Jul 6, 2011 4:17 AM by flexillu10

    File.load(), Do i need it in this function?

    flexillu10 Level 1

      Here is my function that writes downloaded data to a file and tries to open it. I've had a few problems; applications launching for each file so i get several launches of the same application and sometimes files not being opened.

       

      Two questions:

       

      should i be using file.load() before i try to open the file?

       

      are there any obvious improvements that can be made?

       

       

      private function writeFile(event:Event,url:String):void {
           
           var fileStream:FileStream = new FileStream();
                     
           var file:File=File.applicationStorageDirectory.resolvePath("downloads/"+ count + extension);
                          
           fileStream.open(file, FileMode.WRITE);  
                          
           var dataBuffer:ByteArray = new ByteArray();
                          
           event.target.readBytes(dataBuffer, 0, event.target.bytesAvailable);
           fileStream.writeBytes(dataBuffer, 0, dataBuffer.length);
           fileStream.close();
                          
           event.target.close();
                          
                          
           try {
                file.openWithDefaultApplication();
           }
           catch(error:Error) {
                trace("There was a problem opening the file");
           }

      }

       

      Thanks