operloa
operloa

Reputation: 135

Downloading files using the Laravel download function

I am beginner in Laravel.

I have this code:

$fileName = now()->toDateString() . '.docx';
$myFileName = 'Raport glowny'.docx;
        try {
            $objWriter->save(storage_path($fileName));
        } catch (Exception $e) {
        }

        return response()->download(storage_path($fileName));

I have 2 filenames:

$ fileName = now () -> toDateString (). '.Docx'; $ myFileName = 'Main Report'.docx;

$ fileName - the name of the file on the server's disk

$ myFileName - the name of the file with which I would like the file to be saved on the user's disk.

Is it possible to specify the name of the file in the download function?

If so, how to do it?

Upvotes: 0

Views: 80

Answers (2)

R3Z4
R3Z4

Reputation: 59

you can use Response::download($path);

   Route::get('files/{order_id}/{file_name}', function($order_id,$file_name)
{
    $path = storage_path().'/'.'app'.'/assets/OrderFiles/'.$order_id.'/attach/'.$file_name;
    if (file_exists($path)) {
        return Response::download($path);
    }
})->name('GetAttachFile');

Upvotes: 0

Ulrich Thomas Gabor
Ulrich Thomas Gabor

Reputation: 6654

You can pass this name as second parameter:

return response()->download(storage_path($fileName), $myFileName)

See: https://laravel.com/docs/7.x/responses#file-downloads

Upvotes: 1

Related Questions