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


杨__羊羊
浏览 164回答 1
1回答

慕慕森

警告说您没有此操作的模板,这是正确的,因为您不想输出任何网站。首先,您应该将查询结果放入一个数组中,然后将该数组放入内存 csv 并将其发送给用户,如下所示:$fiveMBs&nbsp; &nbsp; &nbsp; = 5 * 1024 * 1024;$outputBuffer = fopen('php://temp/maxmemory:' . $fiveMBs, 'w');foreach ($rows as $row) {&nbsp; &nbsp; 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);
打开App,查看更多内容
随时随地看视频慕课网APP