SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Snippets - Little bits of code to make you happy

Creating a Page Export Function

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.

Hamish Campbell avatar

Hamish Campbell

Hamish is a web developer for Opus International Consultants in New Zealand. He creates tools for managing data online, with a particular interest in geographical information systems. He is also an active contributor to the SilverStripe open source project.

  • Darren-Lee
    09/04/2011 11:54am (3 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 (3 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 (2 years 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.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image njorndare
7 article image Martijn van Nieuwenhoven
8 article image Darren-Lee
9 article image Roman Schmid
10 article image Matt Clegg

View full leaderboard


Advertisement