Snippets - Little bits of code to make you happy
Creating a Page Export Function
Tweet6 May 2009 | | | Supports v2.4, v2.3
Need to create an 'export' function for a page? Do it quickly and cleanly with HTTP::sendFileToBrowser. In your page controller, add a new action 'export':
Page.php
class Page_Controller extends ContentController
{
static $allowed_actions = array(
'export'
);
function export()
{
$filename = "PageExport.html";
$data = $this->renderWith(array('Page_Export'));
HTTP::sendFileToBrowser($data, $filename);
}
}
Now you just need to create Page_Export.ss, and direct the user to the URL "/export/". Your page will be returned to the browser as an attachment to download.
3 Comments
RSS feed for comments on this page RSS feed for all comments
Darren-Lee
09/04/2011 11:54am (2 years ago)
Nice and simple! I am using this for an Estate Agency website that needs to export it's data to in order to import into RightMove.
Hibari
23/06/2011 9:48pm (2 years ago)
Hey Hamish, I really like the way that this function is supposed to work. I've been trying to use it in Silverstripe 2.4.5, and so far I'm not having any luck, every time it's pushing the error in php saying "undefined method HTTP::sendFileToBrowser() any idea on how to fix that?
OPen
18/10/2012 10:19am (7 months ago)
This api is deprecated since 2.3 as mentioned here : http://api.silverstripe.org/2.3/sapphire/misc/HTTP.html#methodsendFileToBrowser
Post a comment ...
You cannot post comments until you have logged in. Login Here.