Ajax Button To Force A Filedownload

Hi there,

I need some help with this. I have an ajax button or do I need an ajax submit button?

After pressing the button a pdf will generated and then I like to force the download of the file - but how?

I read some postings but I have no clue how to solve this…




// this is working so far, the pdf file is created

 $button = CHtml::ajaxButton (

                'PDF erzeugen', 

                array('/plugins/direct?plugin=advPrintScreen&function=newDirectRequest'), 

                // array( 'update' => '.pluginblock'), // replacement option

            );


// this could be the download solution???

            $ajaxOptions = array(

                'type'=>'POST',

                'dataType'=>'json',

                'success'=>'js:function(data){ if(data.result==="success"){  window.location="your_url"

           } }'

            );

 $button = CHtml::ajaxSubmitButton (

                'PDF erzeugen', // the link body (it will NOT be HTML-encoded.)

                array('/plugins/direct?plugin=advPrintScreen&function=newDirectRequest'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.

                $ajaxOptions

            );


 public function newDirectRequest() {

// what I have to fill in my called function?

  $file = 'myfile.pdf';

  $path = 'mypath';

// how to start the dowload of my file?




}







// this should be a solution, but I cant get it to work. Is this not working with ajax?


            $path = realPath (Yii::app ()->request->baseURL) . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR . $file;

            if(file_exists ($path))

            {

                return Yii::app ()->getRequest ()

                    ->sendFile ($file, @file_get_contents ($path));

            }



I read that I maybe have to redirect after the ajax call to the file?

Hope someone can give me some hints how to solve this and let me dive a little bit deeper into ajax stuff.

Cheers Kai