Search code examples
typo3typoscriptfluidextbase

TYPO3: File download in Backend


I would like to integrate a CSV download in the Backend. The CSV file doesn't have to be saved on the server, so just a simple Array-to-CSV for download.

I know using FAL is quite tedious in TYPO3 so I would like to know if there is a simple solution for my issue. Like calling a "download" action an returning a "CSV string" to download ?

I did used this solution for the download action but I am looking for a solution without FAL and without keeping the file on the server.


Solution

  • No need for FAL or saving a file on the server. You can add a custom action in your controller that sets the content-type and disposition headers to treat your request like a download:

    public function exportAction()
    {
        // Just an example on how you could access the downloadable data.
        $records = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_domain_model_table');
        // modify the result to be a csv encoded string, json or whatever you want it to be.
        $data = myConvert($records, 'csv');
    
        header('Content-Type: text/x-csv');
        header('Content-Disposition: attachment; filename="download.csv"');
        header('Pragma: no-cache');
    
        return $data;
    }
    

    Where $data equals a csv encoded array for example.

    What's more interesting is what kind of data you want to be downloadable. To make your data downloadable, setting the header()'s and returning any simple data type should work.