为了账号安全,请及时绑定邮箱和手机立即绑定

SQL查询后的TYPO3 CSV下载

SQL查询后的TYPO3 CSV下载

PHP
杨__羊羊 2021-11-19 17:06:18
我在 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 文件中?并且没有收到警告。
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

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


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


查看完整回答
反对 回复 2021-11-19
  • 1 回答
  • 0 关注
  • 163 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信