4 Replies Latest reply on Sep 17, 2010 6:34 AM by Neobox75

    Downloading multiple files, only one "finishes".

    commadelimited

      I'm working on an application that will download images from a remote website. When selecting one URL the code works great. When I loop over an array containing two or more URLs, only the last file in the array actually downloads. All other files show up in the file system as zero length JPGs. I'm not sure what might be happening, but I'm posting my code in hopes that someone sees something that I don't.

       

      Here's the loop

      for (var p=0; p

      And here's the contents of the downloadFile function.

      function downloadFile(url, fileName) {           var req = new air.URLRequest(url);           stream = new air.URLStream();           stream.addEventListener(air.Event.COMPLETE, writeAirFile);           stream.load(req);                 function writeAirFile(e) {                //air.Introspector.Console.log(e);                     var fileData = new air.ByteArray();                stream.readBytes(fileData,0,stream.bytesAvailable);                var fileStream = new air.FileStream();                fileStream.openAsync(fileName, air.FileMode.WRITE);                fileStream.writeBytes(fileData,0);                fileStream.close();                }      }
        • 1. Re: Downloading multiple files, only one "finishes".
          commadelimited Level 2

          Let's try that again...sorry.

           

          for (var p=0; p<numArr.length; p++) {
               var remoteURL = jsonData[numArr[p]]['large'];
               // where do we want to put downloaded images?
               var localURL = air.File.applicationStorageDirectory.resolvePath( remoteURL.match(/[a-z0-9\._]+$/i) );
               downloadFile(remoteURL, localURL);
          }
          

           

          function downloadFile(url, fileName) {
               var req = new air.URLRequest(url);
               stream = new air.URLStream();
               stream.addEventListener(air.Event.COMPLETE, writeAirFile);
               stream.load(req);
          
               function writeAirFile(e) {
                    //air.Introspector.Console.log(e);     
                    var fileData = new air.ByteArray();
                    stream.readBytes(fileData,0,stream.bytesAvailable);
                    var fileStream = new air.FileStream();
                    fileStream.openAsync(fileName, air.FileMode.WRITE);
                    fileStream.writeBytes(fileData,0);
                    fileStream.close();     
               }
          }
          
          • 2. Re: Downloading multiple files, only one "finishes".
            Neobox75 Level 3

            Hi,

             

            It's beacause you use the same stream variable. Use an Array which store multiple stream variables or wait the first download and then start the second etc...

            • 3. Re: Downloading multiple files, only one "finishes".
              commadelimited Level 2

              That was it. I changed this line:

               

              stream = new air.URLStream();
              
              to this:
              
              
              var stream = new air.URLStream();
              
              and I'm golden. I knew it had to be something simple. Thanks for being my second pair of eyes.

              • 4. Re: Downloading multiple files, only one "finishes".
                Neobox75 Level 3

                U welcome.

                It's good if it works but its not the best way to do that because I think your stream variable could be garbage collected.