You are here

public function DownloadFileController::download_file_direct_download in DownloadFile 8

1 string reference to 'DownloadFileController::download_file_direct_download'
download_file.routing.yml in ./download_file.routing.yml
download_file.routing.yml

File

src/Controller/DownloadFileController.php, line 16

Class

DownloadFileController

Namespace

Drupal\download_file\Controller

Code

public function download_file_direct_download($file_id = null) {
  $file = \Drupal\file\Entity\File::load($file_id);
  $default_headers = file_get_content_headers($file);
  $custom_headers = [
    'Content-Type' => 'force-download',
    'Content-Disposition' => 'attachment; filename="' . $file
      ->getFilename() . '"',
    'Content-Length' => $file
      ->getSize(),
    'Content-Transfer-Encoding' => 'binary',
    'Pragma' => 'no-cache',
    'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0',
    'Expires' => '0',
    'Accept-Ranges' => 'bytes',
  ];
  $headers = array_merge($default_headers, $custom_headers);
  return new BinaryFileResponse($file
    ->getFileUri(), 200, $headers);
}