Flash ActionScript 3: Getting the Total Percent Loaded from BulkLoader

Author Avatar
Written by Digital Surgeons,
• 3 min read

How can I get the total percentage loaded from BulkLoader? The question sounds easy enough, but there are hurdles. BulkLoader doesn’t always start downloading all files simultaneously. This makes it hard to determine the total bytes to be loaded.

The bad News:

  Without knowing the total bytes we can’t calculate a percentage. Each file added to BulkLoader’s queue occupies 1 connection. If there are more files than there are connections, BulkLoader can’t accurately retrieve the sizes of those files waiting for a connection to free up.

  “This means that the percentLoaded will be 0 almost until all items are loaded, and then it will jump abruptly. Worse, each time a new connection is open, the bytesTotal would jump around (it would add the bytesTotal for the new item). To mitigate this issue, bytesTotal will remain 0 until all file sizes are known.” Bulkloader Documentation

  BulkLoader offers some alternates to ward off the angry mob of pitchfork toting developers. But there is not a global percentage of loaded items.

What are my options?

  One option is to load by ratio. In short, if there are 6 items total and 2 items have loaded the ratio loaded is .332. This is helpful to know but doesn’t do much if you want an animated progress bar. The bar would have jumped from 16.6% to 33.2% and so on as additional files are loaded.

  The other option is to use weights. This works by telling BulkLoader the size of the file when adding that file to be loaded. This is difficult in cases where you are adding files inside of a loop, and is really only helpful if you have control over the files and the file sizes don’t change. Hard coding is rarely the best solution.

The default options don’t help us.

  In our case we needed the look of a standard progress bar. The weight option wasn’t applicable which meant we needed to roll up our sleeves and dig through some code. It turns out Bulkloader supplies enough information behind the scenes to compose a moderately accurate percentage.

  At the time this article was written the current revision of BulkLoader is 282. So ours is based on that.

  Get the solution.

  Download our version here.
Example of retrieving total progress

import com.digitalsurgeons.loading.BulkLoader; import com.digitalsurgeons.loading.BulkProgressEvent; var bunch_o_files:Array = new Array("file0.jpg","file1.jpg","file2.jpg","file3.xml","file4.png", "file5.png", "file6.mp3","file7.mp3"); var bl:BulkLoader = new Bulkloader("sample"); bl.addEventListener(BulkProgressEvent.PROGRESS, showPercentage); for(var i:uint = 0; i < bunch_o_files.length; i++){ bl.add(bunch_o_files, {id:"file_""+i}); } bl.start(); function showPercentage(evt:BulkProgressEvent):void { trace(evt.totalPercentLoaded); }