To force a file download when a specific URL (route) is visited, your controller should return a BinaryFileResponse . This method works for both public and private files. File module overview - Drupal
Programmatically downloading a file in Drupal 8 (and later versions like 9 and 10) typically involves two main scenarios: or handling a download after a form submission . drupal 8 download file programmatically
The most robust way to achieve this is by using the BinaryFileResponse class from the Symfony component, which handles the file streaming and necessary HTTP headers for you. 1. Download File from a Controller To force a file download when a specific