How to download file upon submitting a form? I have a form with the submit button.
And I have a file in '/sites/default/files/tmp/my_doc.docx'
I'm trying to download the file upon submitting the form.

Here is my code

public function submitForm(array &amp;$form, FormStateInterface $form_state){
   $filename = '/sites/default/files/tmp/my_doc.docx';
   $response = new BinaryFileResponse($filename);
   $form_state-&gt;setResponse($response);
}


But it doesnt't work properly. 

That is, the file is being downloaded but has the same name that name of php file containing this code.

Could you clarify me, what have I done wrong ?
You need to set the filename with setContentDisposition, as the constructor only takes the file, but doesn't set the name itself:

$response-&gt;setContentDisposition(
    ResponseHeaderBag::DISPOSITION_ATTACHMENT,
    'my_doc.docx'
);


Don't forget to add use Symfony\Component\HttpFoundation\ResponseHeaderBag; at the top of your file.

There is quite a bit more you can do with BinaryFileResponse as well, so check out some of the methods.