bundle/Controller/DownloadController.php
public function downloadFile(Request $request, $contentId, $fieldId, $isInline = false): BinaryStreamResponse
    {
        $content = $this->getSite()->getLoadService()->loadContent(
            $contentId,
            $request->query->get('version'),
            $request->query->get('inLanguage')
        );

        if (!$content->hasFieldById($fieldId) || $content->getFieldById($fieldId)->isEmpty()) {
            throw new NotFoundHttpException(
                $this->translator->trans('download.file_not_found', [], 'ngsite')
            );
        }

        $binaryFieldValue = $content->getFieldById($fieldId)->value;

        if ($binaryFieldValue instanceof BinaryBaseValue) {
            $ioService = $this->ioFileService;
            $binaryFile = $this->ioFileService->loadBinaryFile($binaryFieldValue->id);
        } elseif ($binaryFieldValue instanceof ImageValue) {
            $ioService = $this->ioImageService;
            $binaryFile = $this->ioImageService->loadBinaryFile($binaryFieldValue->id);
        } else {
            throw new NotFoundHttpException(
                $this->translator->trans('download.file_not_found', [], 'ngsite')
            );
        }

        $response = new BinaryStreamResponse($binaryFile, $ioService);
        $response->setContentDisposition(
            (bool) $isInline ? ResponseHeaderBag::DISPOSITION_INLINE :
            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
            str_replace(['/', '\\'], '', $binaryFieldValue->fileName),
            'file'
        );

        if (!$request->headers->has('Range')) {
            $downloadEvent = new DownloadEvent(
                $contentId,
                $fieldId,
                $content->contentInfo->currentVersionNo,
                $response
            );

            $this->dispatcher->dispatch(SiteEvents::CONTENT_DOWNLOAD, $downloadEvent);
        }

        return $response;
    }
public function downloadFile(Request $request, $contentId, $fieldId, $isInline = false): BinaryStreamResponse
    {
        $content = $this->getSite()->getLoadService()->loadContent(
            $contentId,
            $request->query->get('version'),
            $request->query->get('inLanguage')
        );

        if (!$content->hasFieldById($fieldId) || $content->getFieldById($fieldId)->isEmpty()) {
            throw new NotFoundHttpException(
                $this->translator->trans('download.file_not_found', [], 'ngsite')
            );
        }

        $binaryFieldValue = $content->getFieldById($fieldId)->value;

        if ($binaryFieldValue instanceof BinaryBaseValue) {
            $ioService = $this->ioFileService;
            $binaryFile = $this->ioFileService->loadBinaryFile($binaryFieldValue->id);
        } elseif ($binaryFieldValue instanceof ImageValue) {
            $ioService = $this->ioImageService;
            $binaryFile = $this->ioImageService->loadBinaryFile($binaryFieldValue->id);
        } else {
            throw new NotFoundHttpException(
                $this->translator->trans('download.file_not_found', [], 'ngsite')
            );
        }

        $response = new BinaryStreamResponse($binaryFile, $ioService);
        $response->setContentDisposition(
            (bool) $isInline ? ResponseHeaderBag::DISPOSITION_INLINE :
            ResponseHeaderBag::DISPOSITION_ATTACHMENT,
            str_replace(['/', '\\'], '', $binaryFieldValue->fileName),
            'file'
        );

        if (!$request->headers->has('Range')) {
            $downloadEvent = new DownloadEvent(
                $contentId,
                $fieldId,
                $content->contentInfo->currentVersionNo,
                $response
            );

            $this->dispatcher->dispatch(SiteEvents::CONTENT_DOWNLOAD, $downloadEvent);
        }

        return $response;
    }
public function downloadFile ( Request $ request , $ contentId , $ fieldId , $ isInline = false ) : BinaryStreamResponse { $ content = $ this -> getSite ( ) -> getLoadService ( ) -> loadContent ( $ contentId , $ request -> query -> get ( 'version' ) , $ request -> query -> get ( 'inLanguage' ) ) ; if ( ! $ content -> hasFieldById ( $ fieldId ) || $ content -> getFieldById ( $ fieldId ) -> isEmpty ( ) ) { throw new NotFoundHttpException ( $ this -> translator -> trans ( 'download.file_not_found' , [ ] , 'ngsite' ) ) ; } $ binaryFieldValue = $ content -> getFieldById ( $ fieldId ) -> value ; if ( $ binaryFieldValue instanceof BinaryBaseValue ) { $ ioService = $ this -> ioFileService ; $ binaryFile = $ this -> ioFileService -> loadBinaryFile ( $ binaryFieldValue -> id ) ; } elseif ( $ binaryFieldValue instanceof ImageValue ) { $ ioService = $ this -> ioImageService ; $ binaryFile = $ this -> ioImageService -> loadBinaryFile ( $ binaryFieldValue -> id ) ; } else { throw new NotFoundHttpException ( $ this -> translator -> trans ( 'download.file_not_found' , [ ] , 'ngsite' ) ) ; } $ response = new BinaryStreamResponse ( $ binaryFile , $ ioService ) ; $ response -> setContentDisposition ( ( bool ) $ isInline ? ResponseHeaderBag :: DISPOSITION_INLINE : ResponseHeaderBag :: DISPOSITION_ATTACHMENT , str_replace ( [ '/' , '\\' ] , '' , $ binaryFieldValue -> fileName ) , 'file' ) ; if ( ! $ request -> headers -> has ( 'Range' ) ) { $ downloadEvent = new DownloadEvent ( $ contentId , $ fieldId , $ content -> contentInfo -> currentVersionNo , $ response ) ; $ this -> dispatcher -> dispatch ( SiteEvents :: CONTENT_DOWNLOAD , $ downloadEvent ) ; } return $ response ; }
Downloads the binary file specified by content ID and field ID.

Assumes that the file is locally stored

Dispatch \Netgen\Bundle\SiteBundle\Event\SiteEvents::CONTENT_DOWNLOAD only once

@param \Symfony\Component\HttpFoundation\Request $request
@param mixed $contentId
@param mixed $fieldId
@param bool $isInline

@throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException If file or image does not exist

@return \eZ\Bundle\EzPublishIOBundle\BinaryStreamResponse
Downloads the binary file specified by content ID and field ID .