首页 > 解决方案 > SQL查询后的TYPO3 CSV下载

问题描述

我在我的 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 文件中?并且没有收到警告。

标签: phpdoctrinetypo3extbase

解决方案


警告说您没有此操作的模板,这是正确的,因为您不想输出任何网站。

首先,您应该将查询结果放入一个数组中,然后将该数组放入内存 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);

推荐阅读