krischanski
krischanski

Reputation: 21

Return large file as attachment from Extbase Controller action

In one of its actions, my Controller should return a large zip file, which I load from a cloud storage via curl before. It perfectly works with smaller files of 50MB or so, but throws an 500 error for large files of 1GB and aborts while sending files of 300MB or so. The files are completely present in the tmp folder.

This is what I return in my action:

return $this->responseFactory->createResponse()
->withAddedHeader('Content-Type', 'application/zip') 
->withAddedHeader('Content-Transfer-Encoding', 'binary') 
->withAddedHeader('Content-Disposition', 'attachment; filename="submissionId.zip"') 
->withAddedHeader('Content-Length', $length) 
->withBody($this->streamFactory->createStream($content));

I raised the memory_limit of the server to more than 2G, but nothing changed.

Upvotes: 0

Views: 207

Answers (1)

Mogens
Mogens

Reputation: 1181

In TYPO3 11 i run a dynamic zip download for images. The file sizes also vary from 50MB to over 2GB.

I also struggled around with download aborts. This example does now run stable for several years since TYPO3 6. Although it is not implemented as described in the documentation.

public function downloadZipFile()    
{
    
    $zipTmpFile = tempnam("tmp", "zip");
    $zipArchive = new \ZipArchive();
    $zipArchive->open($this->zipTmpFile, \ZipArchive::OVERWRITE);

    // .... adding files to the Zip archive
    // .... and create a download filename
    $downloadFileName = 'MyCoolZipFile';

    $this->zipArchive->close();
    ini_set('memory_limit', '2048M');
    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-Type: application/zip, application/octet-stream');
    header('Content-Disposition: filename="' . $downloadFileName . '.zip"');
    header('Content-length: '.filesize($zipTmpFile));
    header('Cache-control: private');

    echo file_get_contents($zipTmpFile);
    unlink($zipTmpFile);
    exit();

}

Upvotes: 0

Related Questions