Skip to main content
Participant
April 20, 2014
Question

AS3 download() fileReference doesnt work

  • April 20, 2014
  • 1 reply
  • 585 views

Hello all, I have a problem with the location where I put the: fileReference.download(urlRequest, "file.zip"); line. If I test this after the event Click on the button works fine! But if I put this inside a function doesnt work, the code:

package {

          import flash.display.Sprite;

          import flash.display.Graphics;

          import flash.text.TextField;

          import flash.text.TextFormat;

          import flash.text.TextFormatAlign;

          import flash.events.Event;

          import flash.events.ProgressEvent;

          import flash.events.IOErrorEvent;

          import flash.events.MouseEvent;

          import flash.net.FileReference;

          import flash.net.URLRequest;

          import flash.net.URLLoader;

          import flash.net.URLVariables;

          import flash.net.URLLoaderDataFormat;

          import flash.net.URLRequestMethod;

          import flash.external.ExternalInterface;

          public class Main extends Sprite{

                    var proxy:String = "proxy.php?data=";

                    var filename:String = "archivo.zip";

                    var req:URLRequest = new URLRequest(proxy + filename);

                    var file:FileReference = new FileReference();

                    var formattedCode:String;

                    var formattedEmail:String;

                    var emailInput:TextField = new TextField();

                    var codeInput:TextField = new TextField();

                    var inputHeight:Number = new Number(29);

                    var inputWidth:Number = new Number(387);

                    var email_regex:RegExp = /^[a-zA-Z0-9._%+-]+@(?:[a-zA-Z0-9-]+\.)+[A-Z]{2,4}$/i;

                    var formatInput:TextFormat = new TextFormat();

                    var download_btn:Sprite = new Sprite();

                    public function Main() {

                              download_btn.graphics.beginFill(0xFFCC00);

                              download_btn.graphics.drawRect(300, 300, 200, 200);

                              download_btn.graphics.endFill();

                              formatInput.size = 20;

                              formatInput.align = TextFormatAlign.LEFT;

                              emailInput.setTextFormat(formatInput);

                              codeInput.setTextFormat(formatInput);

                              emailInput.type = "input";

                              codeInput.type = "input";

                              emailInput.multiline = false;

                              codeInput.multiline = false;

                              emailInput.width = inputWidth;

                              codeInput.width = inputWidth;

                              emailInput.height = inputHeight;

                              codeInput.height = inputHeight;

                              emailInput.alwaysShowSelection = true;

                              codeInput.alwaysShowSelection = true;

                              emailInput.x = 75;

                              codeInput.x = 75;

                              emailInput.y = inputHeight + (29*3);

                              codeInput.y = inputHeight + (29*4);

                              emailInput.border = true;

                              codeInput.border = true;

                              addChild(emailInput);

                              addChild(codeInput);

                              addChild(download_btn);

                              download_btn.addEventListener(MouseEvent.CLICK, sendData);

                    }

                    private function sendData(e:MouseEvent):void {

                              var codeVar:String = codeInput.text;

                              var emailVar:String = emailInput.text;

                              formattedCode = codeVar.toUpperCase();

                              formattedEmail = emailVar.toLowerCase();

                              if(formattedEmail.match(email_regex) && formattedEmail !== "") {

                                        checkCodeFree();

                              } else {

                                        ExternalInterface.call("console.log", "Bad email address");

                              }

                    }

                    private function checkCodeFree():void {

                              ExternalInterface.call("console.log", "checkCodeFree");

                              var url = "checktrue.php";

                              var loader:URLLoader = new URLLoader;

                              var urlreq:URLRequest = new URLRequest(url);

                              var urlvars:URLVariables = new URLVariables;

                              loader.dataFormat = URLLoaderDataFormat.VARIABLES;

                              urlreq.method = URLRequestMethod.POST;

                              urlvars.codeVar = formattedCode;

                              urlreq.data = urlvars;

                              loader.addEventListener(Event.COMPLETE, completed);

                              ExternalInterface.call("console.log", "Calling loader...");

                              loader.load(urlreq);

                    }

                    private function completed(event:Event):void {

                              ExternalInterface.call("console.log", "completed");

                              var loader2: URLLoader = URLLoader(event.target);

                              var code:String = loader2.data.code;

                              var checked:String = loader2.data.checked;

                              var edition:String = loader2.data.edition;

                              var codetrue:String = loader2.data.codetrue;

                              if(checked == "1" || codetrue == null || codetrue == "") {

                                        if(checked == "1"){

                                                  ExternalInterface.call("console.log", "Code in use: " + checked);

                                        }

                                        if(codetrue == null){

                                                  ExternalInterface.call("console.log", "Code not exist (null): " + codetrue);

                                        }

                                        if(codetrue == ""){

                                                  ExternalInterface.call("console.log", "Code not exist: " + codetrue);

                                        }

                              }

                              else{

                                        ExternalInterface.call("console.log", "true_code");

                                        promptDownload();

                              }

                    }

                    public function promptDownload():void {

                              ExternalInterface.call("console.log", "promptDownload");

                              file.addEventListener(Event.COMPLETE, completeHandler);

                              file.addEventListener(Event.CANCEL, cancelHandler);

                              file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);

                              file.addEventListener(ProgressEvent.PROGRESS, progressHandler);

                              file.download(req, "file.zip");                          // THIS DOWNLOAD DOESNT WORKS

                    }

                    private function cancelHandler(event:Event):void {

                              ExternalInterface.call("console.log", "cancelHandler");

                    }

                    private function progressHandler(event:ProgressEvent):void {

                              ExternalInterface.call("console.log", "progressHandler: name=" + file.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);

                        }

                    private function completeHandler(event:Event):void {

                              ExternalInterface.call("console.log", "completeHandler");

                    }

                    private function ioErrorHandler(event:IOErrorEvent):void {

                              ExternalInterface.call("console.log", "ioErrorHandler");

                    }

          }

}

This topic has been closed for replies.

1 reply

sgrufiAuthor
Participant
April 20, 2014

Is possible download() function of "fileReference" works only in a button event?

kglad
Community Expert
Community Expert
April 21, 2014

for a web-based app it only works in response to a user even like a mouse click or keyboard event. 

When you use this method , consider the Flash Player security model:

  • Loading operations are not allowed if the calling SWF file is in an untrusted local sandbox.
  • The default behavior is to deny access between sandboxes. A website can enable access to a resource by adding a URL policy file.
  • You can prevent a SWF file from using this method by setting the  allowNetworking parameter of the the object and embed tags in the HTML page that contains the SWF content.
  • In Flash Player 10 and Flash Player 9 Update 5, you can only call this method successfully in response to a user event (for example, in an event handler for a mouse click or keypress event). Otherwise, calling this method results in Flash Player throwing an Error exception.