使用 Laravel 下载功能下载文件

Downloading files using the Laravel download function

我是 Laravel 的初学者。

我有这个代码:

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

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

我有 2 个文件名:

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

$ fileName - 服务器磁盘上的文件名

$ myFileName - 我希望将文件保存在用户磁盘上的文件名。

是否可以在下载功能中指定文件名?

如果可以,怎么做?

您可以将此名称作为第二个参数传递:

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

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

你可以使用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');