[英]Laravel response()->download(), not working correctly
Why is this part of code is wrong?为什么这部分代码是错误的?
foreach
loop in directory with-> file names and sizes and download link for each file.目录中的
foreach
循环 -> 文件名和大小以及每个文件的下载链接。
When i ispect download button in chrome i got this in development tool:当我在 chrome 中查看下载按钮时,我在开发工具中得到了这个:
<a href="#" class="btn btn-sm btn-outline-primary btn-block btn-login text-uppercase mb-2"
download="HTTP/1.0 200 OK
0: Content-Type: application/octet-stream
Cache-Control: public
Content-Disposition: attachment; filename=testfile.pdf;
Date: Fri, 27 Nov 2020 13:58:31 GMT
Last-Modified: Fri, 27 Nov 2020 13:58:31 GMT
">Download</a>
when it the button i download file with name:当它按钮时,我下载带有名称的文件:
HTTP_1.0 200 OK_0_Content-Type_ application_octet-stream_Cache-Control_public_.pdf__Date_
Fri, 27 Nov 2020 13_58_31 GMT_Last-Modified_Fri, 27 Nov 2020 13_58_31 GMT__
insteed of testfile.pdf
代替
testfile.pdf
code
foreach($filesDownloadPath as $path) {
$filesDownloads = pathinfo($path)['basename'];
$filesDownloadSize = ConvertFileSize::ConversionFile(filesize($path));
$downloadLink = response()->download(storage_path('app/Files/'.$filesDownloads), $filesDownloads,
$headers);
}
return view('page',["$filesDownloads" => 'fileDownloads',
"$filesDownloadSize" => 'filesDownloadSize',
"$downloadLink" => 'downloadLink'
]);
is problem with varibale $downloadLink
?变量
$downloadLink
有问题吗? with response()->download()
?使用
response()->download()
?
blade
@foreach($filesDownloadPath as $path)
<tr>
<td>{{ $filesDownloads }}</td>
<td>{{ $filesDownloadSize}}</td>
<td><a href="#" class="btn btn-sm btn-outline-primary btn-block btn-login text-uppercase mb-2" download='{{ $downloadLink }}'>Download</a></td>
</tr>
@endforeach
response()->download()
produces a Response
object which must be returned from a controller to do anything. response()->download()
产生一个Response
object 必须从 controller 返回才能执行任何操作。 You'll need to return an array of files names or ids to your template, eg,您需要将一组文件名或 ID 返回到您的模板,例如,
$filesForDownload = [];
foreach($filesDownloadPath as $path) {
$filesForDownload[] = [
'path' => pathinfo($path)['basename'],
'size' => ConvertFileSize::ConversionFile(filesize($path))
];
}
return view('page', ["filesForDownload" => $filesForDownload]);
then in your view:那么在你看来:
@foreach($filesForDownload as $file)
<tr>
<td>{{ $file['path'] }}</td>
<td>{{ $file['size'] }}</td>
<td><a href="/file/{{$file['path']}}" class="btn btn-sm btn-outline-primary btn-block btn-login text-uppercase mb-2">Download</a></td>
</tr>
@endforeach
Next we create a route in laravel pointing to a new method in your controller:接下来,我们在 laravel 中创建一个指向 controller 中的新方法的路由:
Route::get('/file/{filePath}', 'YourController@downloadFile');
and create a method in your controller which returns the response()->download()
并在您的 controller 中创建一个方法,该方法返回
response()->download()
public function downloadFile($filePath) {
return response()->download(storage_path('app/Files/'.$filePath), $filePath);
}
Note that this basic code has absolutely no security checking and could allow any user to download any file within app/Files
(or anywhere on your system depending on your PHP config).请注意,此基本代码绝对没有安全检查,并且可以允许任何用户下载
app/Files
中的任何文件(或系统上的任何位置,具体取决于您的 PHP 配置)。 You can get around that by having a whitelist of files which can be downloaded or storing file info in a database and have them provide the file ID to download it.您可以通过拥有可以下载的文件白名单或将文件信息存储在数据库中并让他们提供文件 ID 来下载它来解决这个问题。
I download like this way我是这样下载的
$file = storage_path()."app/Files/'.$filesDownloads";
if (file_exists($file)) {
$headers = [
'Content-Type' => 'application/pdf',
];
return response()->download($file, "{$filesDownloads}", $headers);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.