Created
March 9, 2019 06:39
-
-
Save derhansen/c56ff4df72d6b83121bea99bd83271cd to your computer and use it in GitHub Desktop.
TYPO3 - Extbase stream file from resourceStorage using streamFile PSR-7 Response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function downloadAction() | |
{ | |
$storage = $this->resourceFactory->getDefaultStorage(); | |
$file = $storage->getFile('test.jpg'); | |
$response = $storage->streamFile($file, true, 'test-filename.jpg'); | |
$this->sendResponse($response); | |
exit(); | |
} | |
protected function sendResponse($response) | |
{ | |
if (!headers_sent()) { | |
if (http_response_code() === 200) { | |
header('HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase()); | |
} | |
foreach ($response->getHeaders() as $name => $values) { | |
header($name . ': ' . implode(', ', $values)); | |
} | |
} | |
$body = $response->getBody(); | |
echo $body->__toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one!! I didn't know about
\TYPO3\CMS\Core\Resource\ResourceStorage::streamFile()
.