1 Reply Latest reply on Jul 7, 2011 2:19 AM by Peter Blazejewicz

    How to properly use ProgressEvent's bytesLoaded and bytesTotal for file download

    bon_t

      Hi All,

      I am using Flex 4, Actionscript 3.

      I am using FileReference's download method, which automatically opens  the "Save As" dialog window (ie. I don't have to explicitly call browse  like for an upload).

      I'd like the label of my ProgressBar component to display
      downloading <%> ...

      In my ProgressEvent handler, I tried

      Code:
           prgbr_fileDownload.label = "downloading %3%%...";
           prgbr_fileDownload.setProgress( event.bytesLoaded, event.bytesTotal );
      But this just displayed "downloading ...". I already set the mode of my ProgressBar component to "manual".

      I then tried
      Code:
      prgbr_fileDownload.label = "downloading " + Math.round( ( event.bytesLoaded/event.bytesTotal ) * 100 ) + "% ...";
      And I was shocked to see very large numbers.

      I then noticed my trace statements displaying the following:
      Code:
      progressHandler name = g2.zip bytesLoaded = 98304 bytesTotal = 98304
      progressHandler name = g2.zip bytesLoaded = 196608 bytesTotal = 98304
      progressHandler name = g2.zip bytesLoaded = 377421 bytesTotal = 98304
      etc.
      progressHandler name = g2.zip bytesLoaded = 17820389 bytesTotal = 98304
      progressHandler name = g2.zip bytesLoaded = 17829381 bytesTotal = 98304
      progressHandler name = g2.zip bytesLoaded = 17838373 bytesTotal = 98304
      etc.
      progressHandler name = g2.zip bytesLoaded = 29940557 bytesTotal = 98304
      progressHandler name = g2.zip bytesLoaded = 30012485 bytesTotal = 98304
      progressHandler name = g2.zip bytesLoaded = 30103874 bytesTotal = 98304
      Hence, obviously my last computation would not work.

      How can I get the percentage downloaded to be displayed correctly?

      Thanks,
      Bonnie