使用 Laravel 下载功能下载文件

我是 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 = '主报告'.docx;


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


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


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


如果可以的话,该怎么办呢?


Helenr
浏览 87回答 2
2回答

白衣非少年

您可以将此名称作为第二个参数传递:return response()->download(storage_path($fileName), $myFileName)

子衿沉夜

您可以使用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');
打开App,查看更多内容
随时随地看视频慕课网APP