rap2hpoutre / fast-excel

🦉 Fast Excel import/export for Laravel
MIT License
2.12k stars 248 forks source link

download() function send a "download" file to browser #260

Open Sicklou opened 2 years ago

Sicklou commented 2 years ago

Laravel version : v8.83.4 fast-excel version : v3.2.0

Hello,

When using the download method, I was expecting to have a file named after the download parameter (here in the example export_test.xlsx).

But it downloads a file named "download" without extension.

return (new FastExcel($collection))->download('export_test.xlsx');

So I took a look into the download function, and this is what works for me, if I add a string to the streamDownload function as second parameter (as the laravel doc suggests : https://laravel.com/docs/8.x/responses#streamed-downloads).

public function download($path, callable $callback = null)
    {
        if (method_exists(response(), 'streamDownload')) {

            return response()->streamDownload(function () use ($path, $callback) {
                self::exportOrDownload($path, 'openToBrowser', $callback);
            }, "export_test.xlsx");
        }
        self::exportOrDownload($path, 'openToBrowser', $callback);

        return '';
    }

Since I couldn't find anyone else with the problem, I was wondering if I was not using it wrong.

skymount-ru commented 2 years ago

Hi, Sicklou! I've used this way with Livewire (it also may be used as a response in web controller).

return response()->streamDownload(function () {
    return (new FastExcel($collection))
        ->export('php://output');
}, sprintf('leads-%s.xlsx', date('Y-m-d')));

Laravel 9.5, PHP 8.1