0

I'm actually trying to send the Content-Length header with my file response (for downloading status bar purproses on client side) using this method :

return response()->header('Content-Length', $fileSize)->file($this->filesPath.$fileName);

But i'm getting an error :

BadMethodCallException: Method Illuminate\Routing\ResponseFactory::header does not exist. in file /www/vendor/laravel/framework/src/Illuminate/Macroable/Traits/Macroable.php on line 113

It seems that the method is not recognized when there is "file()" in the response.

Here are my response headers :

My headers

Note : Apache webserver based on Linux machine running Laravel 8 with PHP 8.1

Aditional informations :

After testing on different environements and with multiples files, i've found out that the Content-Length header is working only with images. I've tested it in Laravel 9 & 8 and still can't send Content-Length header manually. Any other header is working.

  • Does this answer your question? [Download files in laravel using Response::download](https://stackoverflow.com/questions/20415444/download-files-in-laravel-using-responsedownload) – steven7mwesigwa Sep 09 '22 at 08:42

1 Answers1

0

Use the pre-built download() method to send headers

$filesPath = 'path/to/file'; // change this
$fileName = 'file-name'; // change this
$fileSize = 'size'; // change this

$headers = [
    'Content-Length' => $fileSize
];

return response()->download($filesPath, $fileName, $headers);
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • Tried : ` return response()->download($this->filesPath.$fileName, $fileName, [ 'Content-Length' => $fileSize, 'X-FileSize' => $fileSize, ]);` But any of both headers is working.. – Luca ALSINA Sep 12 '22 at 07:04