dougu3290 2019-08-12 07:42
浏览 102
已采纳

SQL-Query后的TYPO3 CSV下载

I created a button in my TYPO3 backend

<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>

It calls the function in my controller

public function redirectDownloadAction()
{
   $this->redirectRepository->getRedirects();
}

and in my repository

public function getRedirects()
{
    $connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
    $queryBuilder = $connectionPool
    ->getQueryBuilderForTable('tx_redirects');
     $csvData = $queryBuilder
        ->select("*")
        ->from('tx_redirects')
        ->execute()
        ->fetchAll();
    return $csvData;
}

I get the correct data and after executing the info The technical reason is: No template was found. View could not be resolved for action "redirectDownload" in class "\Controller\RedirectController".

My question is how can I download the SQL-result in a CSV file by clicking the button? and dont get the warning.

  • 写回答

1条回答 默认 最新

  • douxiong4892 2019-08-12 10:28
    关注

    The warning says you have no Template for this action, what is correct because you dont want to output any website.

    First you should put your query results into an array, than put the array in an memory csv and sent it to the user, something like this:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了