Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

This simple example demonstrates how to download a file. It gets the content from given URI and creates response out of it.


class MessageController extends Controller
* @Method({"GET"})
* @Route("/messages/{messageId}/attachments/{attachmentId}")
* @param int $messageId
* @param int $attachmentId
* @return Response
* @throws HttpResponseException|NotFoundHttpException
public function getAttachmentAction($messageId, $attachmentId)
try {
// Do something with $messageId, $attachmentId

$attachment['contentType'] = 'image/png';
$attachment['name'] = 'test.png';
$attachment['url'] = 'https://www.inanzzz/files/1';

$result = $whateverService->fetch($attachment['url']);

if ($result['responseCode'] != 200) {
throw new HttpResponseException(sprintf('Couldn\'t fetch file from [%s].', $attachment['url']));

$response = new Response();
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', $attachment['contentType']);
$response->headers->set('Content-Disposition', sprintf('attachment;filename=%s', $attachment['name']));

return $response;
} catch (Exception $e) {
throw new NotFoundHttpException($e->getMessage());