Downloading Files with Adobe AIR, HTML and JS Part 2. Adding a Progress Bar with Canvas

Filed under: AIR, JavaScript

comments (5) Views: 10,200

Just a quick follow up to my previous post about downloading files with Adobe AIR. Reader Jason asked "How would you create a progress bar to go with the download." If this is something you've been asking for, then read on. You can also download the complete source for this project.

  • Start off by downloading the source code for the previous article. We're not going to rewrite that code, but we will be adding to it.
  • Unzip the code and import the project into Aptana, ColdFusion Builder, or your flavor of Eclipse based editors.
  • Open the file named DownloadFile.html
  • Add this CSS declaration into the style block at the top of the page #progressBar { border: 1px solid #000000; }
  • Just above the closing form tag, add this line of code Progress: <canvas id="progressBar" width="500" height="20"></canvas>
  • Save this file, and close it.
  • Expand the lib node, then open the file named Application.js
  • Locate the downloadFile function. Add these lines just above the stream.load statement
  • Finally, after the downloadFile function add this new function.

Here's how this works. The URLStream class fires off several events while it's downloading your selected file. We're already listening for the COMPLETE event; it's how we capture the complete data stream and save the file off. But if we take a look at the AIR API we'll see that there's also a PROGRESS event that we can listen for by calling air.ProgressEvent.PROGRESS. The PROGRESS event has two key values passed along with that we're using: bytesLoaded and bytesTotal. Having those two values allow us to calculate just how much of the file has been download.

Finally, because we're using webkit which has excellent Canvas support, we can draw a rectangle which displays our progress. This is a simple implementation, but it could be made as complex as you desired, or just about any shape you can imagine. What sorts of things are doing with AIR and HTML/JS? Are you using the Canvas tag for any portion of your application?

Amazon logo

If this article was interesting, or helpful, or even wrong, please consider leaving a comment, or buying something from my wishlist. It's appreciated!

Related Posts


comments powered by Disqus
coach outlet online jordan 13 grey toe beats by dre cyber monday michael kors black friday beats by dre cyber monday jordan 6 black infrared north face cyber monday michael kors cyber monday north face black friday coach outlet black infrared 23 13s north face cyber monday jordan 6 black infrared north face black friday coach cyber monday jordan 11 legend blue north face cyber monday black infrared 6s lebron 12 north face black friday jordan 11 legend blue louis vuitton outlet jordan 13 grey toe grey toe 13s beats by dre black friday coach black friday jordan 13 grey toe coach cyber monday uggs black friday jordan 13 black infrared 23 uggs cyber monday barons 13s uggs black friday beats by dre cyber monday black infrared 6s jordan 13 bred jordan 13 black infrared 23 north face black friday black infrared 6s jordan 11 legend blue michael kors black friday jordan 13 grey toe coach black friday michael kors black friday michael kors cyber monday beats by dre cyber Monday north face cyber monday coach black friday michael kors cyber monday beats by dre cyber Monday north face black friday beats by dre black friday lululemon black friday uggs black friday jordan 13 bred coach cyber monday beats by dre black friday uggs black friday coach black friday black infrared 6s