denyAccessUnlessGranted("read", $attachment); if ($attachment->isExternal()) { throw new \Exception("You can not download external attachments!"); } if (!$helper->isFileExisting($attachment)) { throw new \Exception("The file associated with the attachment is not existing!"); } $file_path = $helper->toAbsoluteFilePath($attachment); $response = new BinaryFileResponse($file_path); //Set header content disposition, so that the file will be downloaded $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT); return $response; } /** * View the attachment * * @Route("/attachment/{id}/view", name="attachment_view") * @param Attachment $attachment */ public function view(Attachment $attachment, AttachmentHelper $helper) { $this->denyAccessUnlessGranted("read", $attachment); if ($attachment->isExternal()) { throw new \Exception("You can not download external attachments!"); } if (!$helper->isFileExisting($attachment)) { throw new \Exception("The file associated with the attachment is not existing!"); } $file_path = $helper->toAbsoluteFilePath($attachment); $response = new BinaryFileResponse($file_path); //Set header content disposition, so that the file will be downloaded $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_INLINE); return $response; } }