Php Laravel-文件下载
嗨,我正在尝试修复我正在处理的Laravel下载错误。我在控制器中有正确的路线设置和正确的功能。我还能够确认我有权访问该文件,因为我使用完全相同的路径创建了一个文件并返回了它。通过这样做,我能够成功地返回文件的内容。但是,当我尝试从视图中使用按钮并调用控制器函数时,我得到以下错误:Php Laravel-文件下载,php,laravel,laravel-5,Php,Laravel,Laravel 5,嗨,我正在尝试修复我正在处理的Laravel下载错误。我在控制器中有正确的路线设置和正确的功能。我还能够确认我有权访问该文件,因为我使用完全相同的路径创建了一个文件并返回了它。通过这样做,我能够成功地返回文件的内容。但是,当我尝试从视图中使用按钮并调用控制器函数时,我得到以下错误: FileNotFoundException in File.php line 37: The file "The file "2016-04-04_07-21-50 - Pinging host: 192.168.2
FileNotFoundException in File.php line 37:
The file "The file "2016-04-04_07-21-50 - Pinging host: 192.168.2.1
2016-04-04_07-21-50 - Host 192.168.2.1 is up!
2016-04-04_07-21-50 - Pinging host: 192.168.2.2
2016-04-04_07-21-53 - Pinging host: 192.168.2.3 ...
下面是导致此错误的代码:
FileNotFoundException in File.php line 37:
The file "The file "2016-04-04_07-21-50 - Pinging host: 192.168.2.1
2016-04-04_07-21-50 - Host 192.168.2.1 is up!
2016-04-04_07-21-50 - Pinging host: 192.168.2.2
2016-04-04_07-21-53 - Pinging host: 192.168.2.3 ...
show.blade.php
<a class="btn btn-default col-md-12" href="/getDownload/{{ $now }}" role="button">Download Today's Log</a>
public function getDownload($id)
{
$file = File::get("../resources/logs/$id");
$headers = array(
'Content-Type: application/octet-stream',
);
#return Response::download($file, $id. '.' .$type, $headers);
return response()->download($file, $id.'txt', $headers);
}
我能够猜测的是,我得到了一个500 HTTP错误。然而,我的检查并未向我提供任何其他信息。知道发生了什么吗?下载方法的第一个参数应该是文件的路径,而不是文件本身 下载方法可用于生成强制下载的响应 用户浏览器下载给定路径下的文件 资料来源: 试试这个:
public function getDownload($id)
{
// $file = File::get("../resources/logs/$id");
$headers = array(
'Content-Type: application/octet-stream',
);
#return Response::download($file, $id. '.' .$type, $headers);
return response()->download("../resources/logs/$id", $id.'txt', $headers);
}
从文档中:
下载方法可用于生成强制下载的响应
用户的浏览器下载给定路径下的文件
return response()->download($pathToFile,$name,$headers)
遵循以下两个步骤:
`public function download(Blog $blog,$id){
$blog=$blog->find($id);
$headers = array(
'Content-Type: application/octet-stream',
);
$pathToFile=public_path('upload/images/');
$file_name=$blog->cover_image;
$download_name='Download-'.$file_name;
return response()->download($pathToFile.$file_name, $download_name, $headers);
}`
路线
看法
这里,['id'=>1]表示我们将下载主键为1的文件。如果您想下载另一个,只需更改任意n个整数即可
`<a class="btn btn-primary" href="{{ route('file-download',['id'=>1]) }}">Download</a>
`
`文件是否存在?什么是var_dump(File::get('../resources/logs/'.$id))?当您检查
@jakub_jo字符串(59048)时“2016-04-04_07-21-50-ping主机:192.168.2.1 2016-04-04_07-21-50-主机192.168.2.1启动!2016-04-04_07-21-50-ping主机:192.168.2.2。。。另外,fileKDaker评论的所有内容都应该让您走上正确的轨道。您的$now变量可能不是您所认为的。该错误使您看起来像是在传递非常长且奇怪的内容,而不是ID。@KDaker localhost/getDownload/2016-04-05。文件确实存在于我的服务器上,路由也存在:route::get('getDownload/{id}',array('uses'=>'HonoursController@getDownload'));