Search code examples
actionscript-3downloadactionfilereference

As3 FileReference download dynamic url


As3 FileReference download dynamic url not send variable data; Sample:

downloadXls.buttonMode=true;
import flash.net.FileReference;
import flash.events.Event;

var localRef:FileReference;
var fileRequest:URLRequest= new URLRequest("http://[sitename].com/xlsexport.php?id=456");
downloadXls.addEventListener(MouseEvent.CLICK,downloadXlsH);
function downloadXlsH(event:MouseEvent):void{
    localRef = new FileReference();
    localRef.addEventListener(Event.COMPLETE, completeHandler);
    localRef.download( fileRequest, 'myfile.xls' );
}
function completeHandler(e:Event):void{
    trace('OK. Dosya Kaydedildi.');
}

PROBLEM: http://[sitename].com/xls.php?id=456 id variable not send xlsexport.php

Code is just "filename = xlsexport" variable produces a variable I want to send a different "id = 4545 & pass = 87665"


Solution

  • You can do what you want by using URLVariables class.

    Create an ActionScript 3 Class File (*.as) and paste following codes in it:

    package
    {
        import flash.display.Sprite;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.net.FileReference;
        import flash.net.URLRequest;
        import flash.net.URLRequestMethod;
        import flash.net.URLVariables;
    
        public class Download extends Sprite
        {
            var localRef:FileReference;
            var urlVars:URLVariables;
            var fileRequest:URLRequest;
    
            public function Download(downloadXls:*)
            {
                downloadXls.buttonMode=true;
                downloadXls.addEventListener(MouseEvent.CLICK,downloadXlsH);
            }
    
            private function downloadXlsH(event:MouseEvent):void
            {
                localRef = new FileReference();
                localRef.addEventListener(Event.COMPLETE, completeHandler);
    
                urlVars = new URLVariables();
                urlVars.id = 456;
    
                fileRequest = new URLRequest();
                fileRequest.method = URLRequestMethod.GET;
                fileRequest.data = urlVars;
                fileRequest.url = "http://[sitename].com/xlsexport.php";
    
                localRef.download(fileRequest);
            }
    
            private function completeHandler(e:Event):void
            {
                trace('OK. Dosya Kaydedildi.');
            }
        }
    }
    

    And use following line for call it in timeline of your Adobe Flash project (*.fla file).

    import Temp_Example4;
    var temp:Temp_Example4 = new Temp_Example4(downloadXls);
    

    Remember that if you want to add more than one URL variable, you must use following line of code for each one of your URL variables:

    nameOfYourURLVariablesInstance.yourUrlVariableName = yourUrlVariableValue;
    

    something like this:

    urlVars.id = 456;
    

    in sample ActionScript 3 Class File.

    Also, you can download a sample project from following addresses:

    4Shared -> Sample Project

    SendSpace -> Sample Project