You are here

public function WebformResultsExportController::downloadFile in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Controller/WebformResultsExportController.php \Drupal\webform\Controller\WebformResultsExportController::downloadFile()

Download generated CSV file.

Parameters

string $file_path: The paths the generate CSV file.

bool $download: Download the generated CSV file. Default to TRUE.

Return value

\Symfony\Component\HttpFoundation\Response A response object containing the CSV file.

2 calls to WebformResultsExportController::downloadFile()
WebformResultsExportController::file in src/Controller/WebformResultsExportController.php
Returns webform submission results as CSV file.
WebformResultsExportController::index in src/Controller/WebformResultsExportController.php
Returns webform submission as a CSV.

File

src/Controller/WebformResultsExportController.php, line 164

Class

WebformResultsExportController
Controller routines for webform submission export.

Namespace

Drupal\webform\Controller

Code

public function downloadFile($file_path, $download = TRUE) {
  $headers = [];

  // If the file is not meant to be downloaded, allow CSV files to be
  // displayed as plain text.
  if (!$download && preg_match('/\\.csv$/', $file_path)) {
    $headers['Content-Type'] = 'text/plain';
  }
  $response = new BinaryFileResponse($file_path, 200, $headers, FALSE, $download ? 'attachment' : 'inline');

  // Don't delete the file during automated tests.
  // @see \Drupal\webform\Tests\WebformResultsExportDownloadTest
  // @see \Drupal\Tests\webform_entity_print\Functional\WebformEntityPrintFunctionalTest
  if (!drupal_valid_test_ua()) {
    $response
      ->deleteFileAfterSend(TRUE);
  }
  return $response;
}