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 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用