Php Laravel响应()->;下载(),工作不正常

Php Laravel响应()->;下载(),工作不正常,php,laravel,Php,Laravel,为什么这部分代码是错误的foreach在目录中循环->文件名和大小以及每个文件的下载链接 当我在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:

为什么这部分代码是错误的
foreach
在目录中循环->文件名和大小以及每个文件的下载链接

当我在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>
而不是
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'
    ]);
 
varibale
$downloadLink有问题吗?使用
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
@foreach($filesDownloadPath作为$path)
{{$filesDownloads}
{{$filesDownloadSize}
@endforeach

我是这样下载的

$file = storage_path()."app/Files/'.$filesDownloads";
if (file_exists($file)) {
    $headers = [
        'Content-Type' => 'application/pdf',
    ];
    return response()->download($file, "{$filesDownloads}", $headers);
}
response()->download()
生成一个
response
对象,该对象必须从控制器返回才能执行任何操作。您需要向模板返回一组文件名或ID,例如

$filesForDownload = [];
foreach($filesDownloadPath as $path) {
    $filesForDownload[] = [
        'path' => pathinfo($path)['basename'],
        'size' => ConvertFileSize::ConversionFile(filesize($path))
    ];
}

return view('page', ["filesForDownload" => $filesForDownload]);

那么在你看来,

@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

请注意,此基本代码完全没有安全检查,允许任何用户下载
app/Files
中的任何文件(或系统中的任何位置,具体取决于您的PHP配置)。您可以通过拥有可下载文件的白名单或将文件信息存储在数据库中,并让他们提供下载文件的文件ID来解决此问题。

response()->>download()
不创建或返回“链接”,它是一个响应对象(它是返回文件的响应)@lagbox您知道为变量downloadLink分配哪个正确的代码来解决这个问题吗?我不知道您的路由或要使用什么URL,但是您需要一些URL来将浏览器发送到该路由,然后在该路由中返回
响应()->下载(…)
您的
$header
变量在哪里?$headers=array('Content Type:application/pdf',);在foreachi下应用你的好代码,但我在
页面中遇到了这个错误。blade
不能使用Symfony\Component\Finder\SplFileInfo类型的对象作为数组
此行->
{{$file['path']}
对不起,我打错了。在视图中,
$filesDownloadPath
应该是
$filesForDownload
有效!只需再添加一个
{}
最终标记:
a href=“/file/{$file['path']}
。许多感谢的人都会用这些修复来编辑答案!
@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
public function downloadFile($filePath) {
    return response()->download(storage_path('app/Files/'.$filePath), $filePath);
}