title: How do I show the save progress of a FileStream in Flex?
body: <p>I'm making a simple tool that will let the user eventually save a image to the disk.
  To make this, I'm using the <code>FileStream</code> class and its <code>writeBytes()</code> method.</p>
  
  <p>This is working nicelly. The problem occurrs when I tried to show the save progress with a simple <code>mx:ProgressBar</code>. I've tried some approaches, but none seems to work.</p>
  
  <p>Here is the ActionScript piece of code:</p>
  
  <pre><code>private function save(file:File, image:MyImageClass):void {
      var data:BitmapData = new BitmapData(width, height, true, 0x000000);
      image.draw(data, _cols);
      var bytes:ByteArray = new PNGEncoder().encode(data);
      fileStream = new FileStream();
      fileStream.addEventListener(OutputProgressEvent.OUTPUT_PROGRESS, onProgress);
      fileStream.addEventListener(Event.CLOSE, onClose);
      try {
          fileStream.openAsync(file, FileMode.WRITE);
          fileStream.writeBytes(bytes);
      } catch (e:Error) {
          Alert.show("Error trying to save the image: " + e.message);
      } finally {
          fileStream.close();
      }
  }
  
  private function onProgress(event:OutputProgressEvent):void {
      var progressRatio:Number = (1 - (event.bytesPending / event.bytesTotal));
      progressBar.setProgress(progressRatio, 1);
      trace(progressRatio);
      if (event.bytesPending == 0)
          event.target.close();
  }
  
  private function onClose(event:Event):void {
      trace("closed");
  }
  </code></pre>
  
  <p>And the mxml for the progress bar:</p>
  
  <pre><code>&lt;mx:ProgressBar id="progressBar" mode="manual"/&gt;
  </code></pre>
  
  <p>Executing this I got a frozen interface that is released when the file is totally saved and on the console I get all the traces at the same time. The progress bar stays on 0% until the interface is unfrozed and it goes to 100%.</p>
  
  <p>I know that Flash is single thread, but I thought that the <code>FileStream.openAsync()</code> method should do the dirty work to make my interface responsible. It shouldn't be that hard to do this simple and (I think) common task.</p>
  
  <p>The question is: what I'm doing wrong?</p>
  
  <p>Thanks in advance</p>