0 Replies Latest reply on Jan 24, 2013 4:10 PM by Dave Wolfe

    Why does URLStream complete event get dispatched when the file is not finished loading?

    Dave Wolfe Level 1

      I'm writing an AIR kiosk app that every night connects to a WordPress server, gets a JSON file with paths to all the content, and then downloads that content and saves it to the kiosk hard drive. 

       

      There's several hundred files (jpg, png, f4v, xml) and most of them download/save with no problems.  However, there are two f4v files that never get downloaded completely.  The complete event does get dispatched, but if I compare the bytesTotal (from the progress event) vs bytesAvailable (from the complete event) they don't match up; bytesTotal is larger.  The bytesTotal (from the progress event) matches the bytes on the server. 

       

      The bytesLoaded in the progress event never increases to the point that it matches the bytesTotal so I can't rely on the progress event either.  This seems to happen on the same two videos every time. The videos are not very large, one is 13MB and the other is 46MB.  I have larger videos that download without any problems.  

      [edit] After rebooting the compter, the two videos that were failing now download correctly, and now it's a 300kb png file that is not downloading completely.  I'm only getting 312889 of 314349 bytes.

       

      If I paste the url into Firefox it downloads correctly, so it appears to be a problem with Flash/AIR.

      [edit] I just wrote a quick C# app to download the file and it works as expected, so it's definitely a problem with Flash/AIR. 

       

      Here's the code I'm using:

       

      package  {

       

          import flash.display.Sprite;

          import flash.events.Event;

          import flash.events.ProgressEvent;

          import flash.net.URLRequest;

          import flash.net.URLStream;

       

          [SWF(backgroundColor="#000000", frameRate="24", width="640", height="480")]

       

          public class Test extends Sprite {

       

              private var fileSize:Number;

              private var stream : URLStream;

              private var url:String = "http://192.168.150.219/wordpress2/wp-content/uploads/2012/12/John-Butler-clip1.f4v";

       

              public function Test() {

                  if (stage)

                      init();

                  else

                      this.addEventListener(Event.ADDED_TO_STAGE, init);

              }

       

              private function init(e:Event=null):void {

                  this.removeEventListener(Event.ADDED_TO_STAGE, init);

                  stream = new URLStream();

                  stream.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);

                  stream.addEventListener(Event.COMPLETE, onLoadComplete);

                  stream.load(new URLRequest(url));

              }

       

              private function onLoadProgress(event:ProgressEvent):void {

                  fileSize = event.bytesTotal;

                  var percent:Number = event.bytesLoaded / event.bytesTotal * 100;

                  trace(percent + "%");

              }

       

              private function onLoadComplete(event:Event):void {

                  trace("loaded", stream.bytesAvailable, "of", fileSize);

                  // outputs "loaded 13182905 of 13184365"

                  // so why is it "complete" when it isn't finished downloading?

              }

          }

      }