php - 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 文件中?并且没有收到警告。
解决方案
警告说您没有此操作的模板,这是正确的,因为您不想输出任何网站。
首先,您应该将查询结果放入一个数组中,然后将该数组放入内存 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);
推荐阅读
- asp.net - 在 Razor 页面中使用带有树对象的模型绑定和递归渲染?
- vue.js - 在字符串中使用'\ n'的新行在vue js中不起作用
- javascript - 当 javascript 更改文本时,防止 HTML 视口重置
- python - Python Selenium 不允许我登录祖先
- c++ - [cgal]; 使用 OpenMP 的段错误错误并行代码
- sql - 在一个表中查找不与第二个表中的日期范围重叠的日期范围
- reactjs - 如何将任何带有 chakra ui Portal 组件的组件传输到 dom 节点?
- shopify - 获取订单和变体 Shopify 其缓慢
- python - 为什么 exec() 或 os.system 在 pywin32 Windows 服务中不起作用?
- vim - 如何正确缩进vim中的流/节奏文件