检查文件是否存在 if (!file_exists($filePath)) { abort(404, '文件不存在'); } // 设置响应头部 $headers = [ 'Content-Disposition' => 'attachment; filename="filename.ext"', // 设置文件名和下载方式 ]; // 返回响应 return response()->download
return Response::download($file, 'filename.pdf', $headers);}
1returnresponse() 2->json(['name'=>'Abigail','state'=>'CA']) 3->withCallback($request->input('callback')); File Downloads Thedownloadmethod may be used to generate a response that forces the user's browser to download the file at the given path. Thedownloadmethod accepts a file ...
Route::get('/stream', function () { return response()->streamDownload(function () { echo file_get_contents("https://github.com/laravel/laravel/blob/master/readme.md"); }, 'laravel-readme.md'); }); 注意:要实现流式下载,在调用这个方法时必须指定第二个文件名参数,否则内容会直接显示在浏...
1returnresponse()->download($pathToFile); 2 3returnresponse()->download($pathToFile,$name,$headers); 4 5returnresponse()->download($pathToFile)->deleteFileAfterSend(true); Symfony HttpFoundation, which manages file downloads, requires the file being downloaded to have an ASCII file name. ...
All routes and controllers should return a response to be sent back to the user's browser. Laravel provides several different ways to return responses. The most basic response is returning a string from a route or controller. The framework will automatically convert the string into a full HTTP...
//路由对应的控制器方法publicfunctiondownloadFile(Request$request){//省略生成文件部分...returnresponse()->download($pathToFile)->deleteFileAfterSend(true);} 我所期望的是,前端调用此接口后,浏览器开始下载对应文件,并在文件下载完成后删除源文件。
$response=Response::make("hello world"); $response->headers->set('Content-type','text/plain'); return $response; }); 如果需要访问Response类的方法,但又要返回一个视图作为响应的内容,通过使用Response::view方法可以很容易实现: returnResponse
Route::get("/response/download" , 'ResponseController@download') ; 2> 使用response()->download(filename)执行下载,这里服务器上的filename的路径,都是针对public/index.php设置的 5、进行路由的跳转,可以跳网站内部路径,也可以外网路径 1> 新建路由匹配规则: Route::get("/response/redirect" , 'Response...
一般来说,你不需要从路由方法返回简单的字符串或数组。而是需要返回整个 Illuminate\Http\Response 实例或 视图。当返回整个 Response 实例时,Laravel 允许自定义响应的 HTTP 状态码和响应头信息。Response 实例继承自 Symfony\Component\HttpFoundation\Response 类,该类提供了丰富的构建 HTTP 响应的方法:...