Thursday, December 19, 2013

Generate XML using controller and force it to sent as download

Hi,

Goal: I have a button on a page. When user clicks it should generate a XML file and make it download (instead of opening in browser window).
Cake version: 1.3

So I create a HTML link like this:

<?php echo $html->link( __('Generate XML file', true), array('action'=>'generateXMLFile','ext'=>'xml'), 
array('class'=>'button','target'=>'_blank') ); ?>

Which outputs a simple HTML link. 

I have added in Routes:

      Router::parseExtensions('json','xml');

My action: 

function generateXMLFile(){
// Configure::write('debug',0);
// Only XML Requests
if(!$this->RequestHandler->isXml()){
die();

                   }
}


My view:

<response>
<posts>
<Post>
<id>test 234</id>
</Post>
</posts>
</response>

My Layout:

<?php header('Content-type: text/xml');?>
<?php echo $this->Xml->header(); ?>
<?php echo $content_for_layout; ?>


Now when I click the link the XML is sent as output to the screen. Clicking with right mousebutton ->save as.. makes it possible to save the file as XML. 
Headers are correctly set as text/xml

How can i prevent this from outputting to the screen but output it to a file/stream for download ?

thanks 


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/groups/opt_out.

No comments: