简体   繁体   English

Laravel 7 浏览器不下载文件

[英]Laravel 7 Browser doesn't download file

I'm running Xampp on my windows 10 machine, and I have a file called files.txt inside my directory project.我在 Windows 10 机器上运行 Xampp,我的目录项目中有一个名为files.txt的文件。

[...]storage\\app\\public [...]存储\\应用程序\\公共

My config/filesystem.php looks like:我的config/filesystem.php看起来像:

    'disks' => [

    'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
        'permissions' => [
            'file' => [
                'public' => 0664,
                'private' => 0600,
            ],
            'dir' => [
                'public' => 0775,
                'private' => 0700,
            ],
        ],
    ],

On my controller I am calling the file to download在我的控制器上,我正在调用要下载的文件

return response()->download(storage_path("app\public\\files.txt"),'down.txt');

I am receiving 200 status Ok, the headers content I receive:我收到 200 状态 好的,我收到的标题内容:

Content-Disposition: attachment; Content-Disposition:附件; filename=files.txt , on response on the browser Xhr devtools I could see the content of the file, but unhappy the download don't happen. filename=files.txt ,在浏览器 Xhr devtools 上的响应中,我可以看到文件的内容,但不幸的是下载没有发生。

Can you try to specify header :您可以尝试指定 header 吗:

$content = storage_path("app\public\files.txt");
$fileName = 'down.txt';
$headers = [
  'Content-type' => 'text/plain', 
  'Content-Disposition' => sprintf('attachment; filename="%s"', $fileName),
  'Content-Length' => strlen($content)
];   

return response()->download($content,$fileName, $headers);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM