SQL-查询后TYPO3 CSV下载
TYPO3 CSV download after SQL-Query
我在 TYPO3 后端创建了一个按钮
<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>
它调用了我控制器中的函数
public function redirectDownloadAction()
{
$this->redirectRepository->getRedirects();
}
在我的资料库中
public function getRedirects()
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool
->getQueryBuilderForTable('tx_redirects');
$csvData = $queryBuilder
->select("*")
->from('tx_redirects')
->execute()
->fetchAll();
return $csvData;
}
我得到了正确的数据并在执行信息后
The technical reason is: No template was found. View could not be resolved for action "redirectDownload" in class "\Controller\RedirectController".
我的问题是如何通过单击按钮将 SQL-结果下载到 CSV 文件中?并且没有得到警告。
警告说您没有此操作的模板,这是正确的,因为您不想输出任何网站。
首先,您应该将查询结果放入一个数组中,然后将数组放入内存 csv 中并将其发送给用户,如下所示:
$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);
我在 TYPO3 后端创建了一个按钮
<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>
它调用了我控制器中的函数
public function redirectDownloadAction()
{
$this->redirectRepository->getRedirects();
}
在我的资料库中
public function getRedirects()
{
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool
->getQueryBuilderForTable('tx_redirects');
$csvData = $queryBuilder
->select("*")
->from('tx_redirects')
->execute()
->fetchAll();
return $csvData;
}
我得到了正确的数据并在执行信息后
The technical reason is: No template was found. View could not be resolved for action "redirectDownload" in class "\Controller\RedirectController".
我的问题是如何通过单击按钮将 SQL-结果下载到 CSV 文件中?并且没有得到警告。
警告说您没有此操作的模板,这是正确的,因为您不想输出任何网站。
首先,您应该将查询结果放入一个数组中,然后将数组放入内存 csv 中并将其发送给用户,如下所示:
$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);