I have this form submit:

public function submitForm(array &$form, FormStateInterface $form_state){

//  File download
    $sFileName = 'JdP-Adhesion.pdf';
    $sFileNameWithPath = 'sites/default/files/' . $sFileName;
    $response = new BinaryFileResponse($sFileNameWithPath);

//  Redirection to front page

I can't make the two sets of code work together.
What is wrong with my code?

  • I don’t think this is possible.
    – cilefen
    Oct 31, 2023 at 22:50
  • 1
    I agree, I don't think it's possible, as the download is a response, and the redirect is a different response. You could redirect to a page that also forces the download maybe.
    – Jaypan
    Nov 1, 2023 at 9:54
  • I think @Jaypan comment is right. It is possible to redirect to a page which then starts the download. That also means the page to which users are redirected must be able to start the download; redirecting to a page provided by Drupal core would not start the file download.
    – apaderno
    Nov 1, 2023 at 11:55


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.