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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line