Thursday, January 2, 2014

Re: Generate XML using controller and force it to sent as download

It can be done with regular PHP, so it can be done with CakePHP.

The MediaView is a view class uses specifically for downloading files, and media, rather than viewing in the browser.  However, this usually requires that the file is locally available on the disk, and this isn't happening in your case.

However, looking at the MediaView and related processing, we can see that you need the Content-Disposition header entry, to force an attachment, with a particular file name.

So, using a cheats method, include the following in your layout:

<?php header('Content-Disposition: attachment; filename="myxml.xml"'); ?>

Though you may want to using something a bit more CakePHP friendly than the raw header() command.

It's been a while since I've done development in CakePHP 1.3, however in CakePHP 2.4, you can specify $this->response->download($filename); to write that header.

Regards
Reuben Helms

On Thursday, 2 January 2014 21:43:03 UTC+10, Willem wrote:
Still haven't figured this out. Is this at all possible with CakePHP? I can't imagine it would not be possible.

thanks




On Thursday, December 19, 2013 5:16:50 PM UTC+1, Willem wrote:
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: