猿问

如何在laravel中下载上传的文件

我想创建一个函数来将上传的文件下载到存储中,但它总是显示错误代码:文件“D:\xampp php 7.2.2\htdocs\siapKerja_laravel\public\files/22”不存在


public function show($idFile)

 { 

    $file = public_path(). "/files/";

    return Response()->download($file.$idFile);$

 } 


Route::get('/verifikasi/pkwt/download/{id}', 'FileController@show')->name('downloadFile');


慕娘9325324
浏览 233回答 2
2回答

德玛西亚99

我希望它对你有帮助。我在“文件”文件夹中上传了文件,而不是在存储文件夹中。您只需根据 that.set 您的 content_types 根据您上传的文件进行更改。我的控制器代码是public function  openPdf(Request $request,$docFile){       $file='/var/www/html/laravelQuiz/public/files/'.$docFile;     $extension = explode(".",$docFile);     foreach($extension as $extensions){        if($extensions == 'pdf'){           $content_types =  'application/pdf';        }elseif($extensions == 'doc'){           $content_types =  'application/msword';        }elseif($extensions == 'txt'){            $content_types =   'application/octet-stream';        }elseif($extensions == 'docx'){            $content_types =     'application/vnd.openxmlformats-officedocument.wordprocessingml.document';        }elseif($extensions == 'ppt'){            $content_types = 'application/vnd.ms-powerpoint';        }elseif($extensions == 'odt'){            $content_types = 'application/vnd.ms-powerpoint';        }elseif($extensions == 'txt'){            $content_types = 'text/plain';        }     }     $content = file_get_contents($file);                    return Response::make($content, 200, array('content-type'=>$content_types));}

慕村225694

您需要提供文件的完整 url。尝试添加文件扩展名:$extension = ".png" // or whatever type file isreturn Response()->download($file.$idFile.$extension);保存文件时,您还可以在存储文件时获取扩展名:$extension = $request->file('file')->extension();
随时随地看视频慕课网APP
我要回答