How to keep the file name when downloading it?

0

I have a small problem with the name of the files when they are downloaded. So I release them

public function expensesAndTravel(){

   $file = storage_path() . '\app\excel\REPORTE DE GASTOS Y VIATICOS.xlsx';
   $headers = ['Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];

   return response()->file($file, $headers);

}

The file is downloaded without problem, but the name of the file takes it from how it declares my route, in this case

Route::get('files/expenses-travel', array(
   'as'   => 'files.expenses.travel',
   'uses' => 'FilesController@expensesAndTravel'
));

It is downloaded with the names of expenses-travel , in the documentation of laravel I find nothing with respect to maintaining the name, however if you can with the method response->donwload() , but with the file() that is the one I need I can not, the only alternative I found is to put the full name of the file as the name of my route. Is there any way to keep the name from my controller and not take the name of the route?

    
asked by Edwin Aquino 25.09.2018 в 15:11
source

1 answer

2

The traditional PHP way is:

 header('Content-Disposition: inline; filename='.$nombreDeArchivo);

In your case I would try:

$headers = [
  'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  'Content-Disposition' => 'inline; filename='. $nombreDeArchivo
];

return response()->file($file, $headers);
    
answered by 25.09.2018 / 15:21
source