Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在启动下载之前在laravel中设置标头_Php_Laravel - Fatal编程技术网

Php 如何在启动下载之前在laravel中设置标头

Php 如何在启动下载之前在laravel中设置标头,php,laravel,Php,Laravel,我想在启动下载之前设置标题 以前我用普通php做过类似的事情: header('Content-Description: File Transfer'); header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="'.basename($path).'"'); header('Expires: 0'); head

我想在启动下载之前设置标题

以前我用普通php做过类似的事情:

      header('Content-Description: File Transfer');
      header('Content-Type: application/pdf');
      header('Content-Disposition: attachment; filename="'.basename($path).'"');
      header('Expires: 0');
      header('Cache-Control: must-revalidate');
      header('Pragma: public');
      header('Content-Length: ' . filesize($path));
      readfile($path);
现在,我仍然希望能够设置相同的标题,调用laravel下载函数并传递我的标题,例如:

return response()->download($pathToFile, $name, $headers);
其中$headers变量应包含我的标题。任何曾经这样做过的人。

来自

在将响应发送回用户之前,您可以使用header方法将一系列头添加到响应中:

return response($content)
            ->header('Content-Type', $type)
            ->header('X-Header-One', 'Header Value')
            ->header('X-Header-Two', 'Header Value');
return response($content)
            ->header('Content-Type', $type)
            ->header('X-Header-One', 'Header Value')
            ->header('X-Header-Two', 'Header Value');
或者,您可以使用withHeaders方法指定要添加到响应中的标题数组:

return response($content)
            ->withHeaders([
                'Content-Type' => $type,
                'X-Header-One' => 'Header Value',
                'X-Header-Two' => 'Header Value',
            ]);
return response($content)
            ->withHeaders([
                'Content-Type' => $type,
                'X-Header-One' => 'Header Value',
                'X-Header-Two' => 'Header Value',
            ]);
就这么简单

$headers =[
        'Content-Description' => 'File Transfer',
        'Content-Type' => 'application/pdf',
    ];

return \Response::download($pathToFile, $name,$headers);

将标题附加到响应

请记住,大多数响应方法都是可链接的,允许流畅地构造响应实例。例如,在将响应发送回用户之前,您可以使用header方法将一系列头添加到响应中:

return response($content)
            ->header('Content-Type', $type)
            ->header('X-Header-One', 'Header Value')
            ->header('X-Header-Two', 'Header Value');
return response($content)
            ->header('Content-Type', $type)
            ->header('X-Header-One', 'Header Value')
            ->header('X-Header-Two', 'Header Value');
或者,您可以使用withHeaders方法指定要添加到响应中的标题数组:

return response($content)
            ->withHeaders([
                'Content-Type' => $type,
                'X-Header-One' => 'Header Value',
                'X-Header-Two' => 'Header Value',
            ]);
return response($content)
            ->withHeaders([
                'Content-Type' => $type,
                'X-Header-One' => 'Header Value',
                'X-Header-Two' => 'Header Value',
            ]);
点击这个链接,它将帮助你

这很有帮助,太棒了@先生Pyramid@SamuelThomas很高兴!这很有帮助,太棒了@Mohamed Kawsara这很有帮助,太棒了@Umair MehmoodIts好的兄弟,永远在这里帮助你。如果你喜欢就竖起大拇指。。