Aşağıdaki örnek ile bir dosyanın nasıl download edileceğini göreceğiz. Verilen URL'den gerekli bilgiyi alıp, response olarak bize sunar.


Örnek


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']));
$response->setStatusCode($result['responseCode']);
$response->sendHeaders();
$response->setContent($result['responseContent']);

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