I created a button in my TYPO3 backend
<f:link.action class="btn btn-default" action="redirectDownload"
additionalAttributes="{role: 'button'}">
<core:icon identifier="actions-system-extension-download"/>
<f:translate key="redirect_download" />
</f:link.action>
It calls the function in my controller
public function redirectDownloadAction()
{
$this->redirectRepository->getRedirects();
}
and in my repository
public function getRedirects()
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool
->getQueryBuilderForTable('tx_redirects');
$csvData = $queryBuilder
->select("*")
->from('tx_redirects')
->execute()
->fetchAll();
return $csvData;
}
I get the correct data and after executing the info The technical reason is: No template was found. View could not be resolved for action "redirectDownload" in class "\Controller\RedirectController".
My question is how can I download the SQL-result in a CSV file by clicking the button? and dont get the warning.
The warning says you have no Template for this action, what is correct because you dont want to output any website.
First you should put your query results into an array, than put the array in an memory csv and sent it to the user, something like this:
$fiveMBs = 5 * 1024 * 1024;
$outputBuffer = fopen('php://temp/maxmemory:' . $fiveMBs, 'w');
foreach ($rows as $row) {
fputcsv($outputBuffer, $row, ';', '"');
}
rewind($outputBuffer);
$content = utf8_decode(stream_get_contents($outputBuffer));
header('Content-Type: text/csv');
header('Content-Length: ' . strlen($content));
header('Content-Disposition: attachment;filename="' . $filename . '.csv"');
die($content);