14 Replies Latest reply on Mar 22, 2010 8:57 AM by tatiana1

    Downloading problem with AS2

    tatiana1 Level 1

      Hi all,

       

      I have an issue which has been driving me mad all morning. I'm trying to download a file from a remote server. I use the following code:

       

      import flash.net.FileReference;
      var listener:Object = new Object();
      var fileRef:FileReference = new FileReference();
      fileRef.addListener(listener);
      var url:String = "http://www.mysite.com/myFolder/myFile.jpg";
      if(!fileRef.download(url)) trace("dialog box failed to open.");

       

      If I execute this code, it works just fine. If I insert this code inside a sendAndLoad, it stops working. Here's an example:

       


      import flash.net.FileReference;

      c = new LoadVars();
      r = new LoadVars();
      r.onLoad = function(ok)
      {
           var listener:Object = new Object();
           var fileRef:FileReference = new FileReference();
           fileRef.addListener(listener);
           var url:String = "http://www.mysite.com/myFolder/myFile.jpg";
           if(!fileRef.download(url)) trace("dialog box failed to open.");

      }

      c.a = 1;
      c.sendAndLoad("http://www.mysite.com/myPhp.php", r, "POST");

       

       

      Does anyone have any idea as to why it doesn't work?

       

      Thank you!!!

        • 1. Re: Downloading problem with AS2
          kglad Adobe Community Professional & MVP

          either your onLoad isn't being called or you have a local variable that's problematic.  it's hard to tell because neither code snippet makes sense unless there's more code somewhere.

           

          to start, where's your listener code?

          • 2. Re: Downloading problem with AS2
            Tatiana Russo

            Hi and thanks for your reply. The code is part of a larger software I'm working on, but basically, the code I attached is just that. If I create an empty AS2 project and paste the first code, it works just fine the way it is. If I add the encasulating sendAndLoad code calling on a PHP routine to do just about anything from a query to whatever function, the code no longer downloads the image file. I eliminated the listener code since I saw that none of the routines was ever executed. I tried "upload" and it works just fine...it just seems to have a problem with "download" and I can't figure out why.

            • 3. Re: Downloading problem with AS2
              kglad Adobe Community Professional & MVP

              you probably have a cross-domain security issue and your php file is not executing.

              • 4. Re: Downloading problem with AS2
                Tatiana Russo Level 1

                Unfortunately, that's not the problem. I've already checked into that. The PHP executes properly. It returns a list of the images I need to download and that is why I have the download routine within the onLoad. The onLoad returns properly, but somewhow the download still fails to execute. I've traced before the sendAndLoad and inside the onLoad and both trace properly. The download just doesn't take place and I can't figure out why. The SWF and the PHP are both inside the same folder as all the rest of the SWF's and PHP's which execute normally. I haven't added anything new and haven't moved anything anywhere else outside the sandbox, so I just can't figure out what is happening.

                • 5. Re: Downloading problem with AS2
                  kglad Adobe Community Professional & MVP

                  if your onload() was called, your download() would be called.

                   

                  what do the following trace() functions reveal:

                   

                  import flash.net.FileReference;

                  c = new LoadVars();
                  r = new LoadVars();
                  r.onLoad = function(ok)
                  {

                  trace(ok);
                       var listener:Object = new Object();
                       var fileRef:FileReference = new FileReference();
                       fileRef.addListener(listener);
                       var url:String = "http://www.mysite.com/myFolder/myFile.jpg";
                       if(!fileRef.download(url)){

                  trace("dialog box failed to open.");

                  } else {

                  trace("opened");

                  }
                  }

                  c.a = 1;
                  c.sendAndLoad("http://www.mysite.com/myPhp.php", r, "POST");

                  • 6. Re: Downloading problem with AS2
                    Tatiana Russo Level 1

                    I get "ok" and "dialog box failed to open"

                    • 7. Re: Downloading problem with AS2
                      kglad Adobe Community Professional & MVP

                      i can't duplicate the problem.  what's the url to your php file?

                      • 8. Re: Downloading problem with AS2
                        Tatiana Russo Level 1

                        The PHP file is in the same folder as the SWF I'm executing and the JPG file is in a subfolder. Maybe it's just me who's gone nuts. I'll try it again first thing Monday morning and get back to you...Thanks again for your help!

                        • 9. Re: Downloading problem with AS2
                          tatiana1 Level 1

                          Hi, I'm back again.

                           

                          I've tried every possible combination, but I get the same results. Here's the code:

                           

                           

                           

                          import mx.controls.Alert;
                          import flash.net.FileReference;

                          System.security.allowDomain("www.mysite.com");

                           

                          //  ***************************************************************************************** ***************************************************************

                          // FIRST TRY  <------- GETS EXECUTED IF I RUN THE SWF, NOT IF I RUN THE HTML

                          //  ***************************************************************************************** ***************************************************************

                          var listener:Object = new Object();

                          listener.onSelect = function(file:FileReference):Void { trace("onSelect: " + file.name); }
                          listener.onCancel = function(file:FileReference):Void { trace("onCancel"); }
                          listener.onOpen = function(file:FileReference):Void { trace("onOpen: " + file.name); }
                          listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void { trace("onProgress with bytesLoaded: " + bytesLoaded + " bytesTotal: " + bytesTotal); }
                          listener.onComplete = function(file:FileReference):Void { trace("onComplete: " + file.name); }
                          listener.onIOError = function(file:FileReference):Void { trace("onIOError: " + file.name); }
                          var downloadItem:FileReference = new FileReference();
                          downloadItem.addListener(listener);
                          if(!downloadItem.download("http://www.adobe.com/platform/whitepapers/platform_overview.pdf")) { trace("dialog box 1 failed to open."); }  

                          // ***************************************************************************************** ***************************************************************

                           

                              c = new LoadVars();

                              r = new XML();
                              r.ignoreWhite = true;
                             
                              r.onLoad = function (ok)
                              {
                                  delete this.onLoad;
                                  var nodes = this.firstChild.childNodes;


                                 trace(nodes[1].firstChild.nodeValue);    <-------- GETS EXECUTED IF I RUN THE HTML, NOT IF I RUN THE SWF

                           

                          //  ***************************************************************************************** ***************************************************************

                          // SECOND TRY  <------ NEVER GETS EXECUTED

                          //  ***************************************************************************************** ***************************************************************

                          var listener:Object = new Object();

                          listener.onSelect = function(file:FileReference):Void { trace("onSelect:  " + file.name); }
                          listener.onCancel = function(file:FileReference):Void {  trace("onCancel"); }
                          listener.onOpen = function(file:FileReference):Void { trace("onOpen: " +  file.name); }
                          listener.onProgress = function(file:FileReference, bytesLoaded:Number,  bytesTotal:Number):Void { trace("onProgress with bytesLoaded: " +  bytesLoaded + " bytesTotal: " + bytesTotal); }
                          listener.onComplete = function(file:FileReference):Void {  trace("onComplete: " + file.name); }
                          listener.onIOError = function(file:FileReference):Void {  trace("onIOError: " + file.name); }
                          var downloadItem:FileReference = new FileReference();
                          downloadItem.addListener(listener);
                          if(!downloadItem.download("http://www.adobe.com/platform/whitepapers/platform_overview.pdf"))  { trace("dialog box 2 failed to open."); }

                          //  ***************************************************************************************** ***************************************************************

                           

                               }

                           

                          c.a = 1;
                          c.sendAndLoad("http://www.mysite.com/myphp.php", r, "POST");

                           

                           

                           

                          Here's the deal:

                           

                          1) I execute the SWF alone ---> the saveFile dialog appears. No trace messages appear. The 'download' that's getting executed is the one labeled FIRST TRY.

                           

                          2) I execute the HTML ---> No saveFile dialog appears, but I get 2 trace messages telling me that dialog boxes 1 and 2 have failed to open and I get the trace containing the value returned by the PHP

                           

                           

                          I've tried this from two different computers, one at home, the other at the office and get the same results. What am I doing wrong??? I appreciate all your help.

                          • 10. Re: Downloading problem with AS2
                            kglad Adobe Community Professional & MVP

                            In Flash Player 9, ActionScript could perform uploads and               downloads at any time. With Flash Player 10, the FileReference.browse and FileReference.download operations may be               initiated only through ActionScript that originates from user  interaction. This               includes actions such as clicking the mouse or pressing the  keyboard.

                            • 11. Re: Downloading problem with AS2
                              tatiana1 Level 1

                              Great, so I'm not going nuts.

                               

                              Ok, so how do I go about allowing a a user to download something without necessarily waiting for him to click something?

                              • 12. Re: Downloading problem with AS2
                                tatiana1 Level 1

                                P.S. Wait a minute! If I have to wait for user interaction in order to execute a download, why is the first download executing, while the second one isn't? If this were the case, neither should execute...

                                • 13. Re: Downloading problem with AS2
                                  kglad Adobe Community Professional & MVP

                                  test on a server.


                                  • 14. Re: Downloading problem with AS2
                                    tatiana1 Level 1

                                    Ok, I've done it, but the results haven't changed.

                                     

                                    I've finally figured out a few things. If you create a movieClp named "downloadFiles", Flash doesn't return an error, but it doens't let you load it for some reason. I've checked out the label and it doesn't seem to be a keyword of any sort. Solved that mystery.

                                     

                                    Now here is what I was trying to do: I need to retrieve a list of files to download from a mySQL database and then I need to download each letting the user choose the destination folder. If this new security issue of Flash Player 10 impedes me from doing a batch download, will I have to put a "download" button next to each file and have the user click it for each single download to begin? That's a pretty silly security update. How are we supposed to perform batch downloading now? It's bad enough the user has to pick a destination folder each time...