Download file programmatically I have a custom module where I need to create a file and download it on form submission. So far, I managed to created the file and save it in public folder, but I cannot find a way to download it to client. This is the code I wrote so far.

public function submitForm(array &amp;$form, FormStateInterface $form_state) {
  $file_name = $date.'-event.ics';
  $cal_file = file_unmanaged_save_data('file content',"public://cal/$file_name",FILE_EXISTS_RENAME);
  return new RedirectResponse(file_create_url("public://cal/211443-event.ics"));
}
You can return a BinaryFileResponse from a controller, or as 4k4 has added:
$response = new BinaryFileResponse(&quot;public://cal/211443-event.ics&quot;);
$form_state-&gt;setResponse($response);