denyAccessUnlessGranted('read', $attachment); if ($attachment->isExternal()) { throw new \RuntimeException('You can not download external attachments!'); } if (!$helper->isFileExisting($attachment)) { throw new \RuntimeException('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 * @param AttachmentHelper $helper * @return BinaryFileResponse * @throws \Exception */ public function view(Attachment $attachment, AttachmentHelper $helper) { $this->denyAccessUnlessGranted('read', $attachment); if ($attachment->isExternal()) { throw new \RuntimeException('You can not download external attachments!'); } if (!$helper->isFileExisting($attachment)) { throw new \RuntimeException('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; } }