简体   繁体   English

Laravel 5.7 Storage 下载 CSV 文件添加了 .txt 扩展名

[英]Laravel 5.7 Storage download CSV file adds a .txt extension

In Laravel 5.7 I'm using a simple route to allow a user to download a local stored csv file.在 Laravel 5.7 中,我使用一个简单的路由来允许用户下载本地存储的 csv 文件。

Route::get('download/{file}', function($file) {
            return Storage::disk('s3')->download($file, $file, ['Content-Type: text/csv"']);
           })->name('download');

And in my Blade template :在我的 Blade 模板中:

<a href="{{ Route('download', ['file' => 'import.csv']) }}" class="btn btn-primary btn-sm btn-line btn-rect" data-original-title="" title=""> <i class="fa fa-download" aria-hidden="true"></i>

When user clicks on the link, Laravel is finding import.csv file into s3 disk and allow user to download the file but it adds a .txt extension.当用户点击链接时,Laravel 会在 s3 磁盘中找到 import.csv 文件并允许用户下载该文件,但它添加了 .txt 扩展名。 So the user is downloading import.csv.txt and this file corresponds to import.csv data.所以用户正在下载import.csv.txt,这个文件对应import.csv数据。

How can I prevent Laravel to add a .txt extension to the CSV file ?如何防止 Laravel 向 CSV 文件添加 .txt 扩展名? I tried return Storage::disk('s3')->download($file, $file);我试过return Storage::disk('s3')->download($file, $file); and return Storage::disk('s3')->download($file);return Storage::disk('s3')->download($file); and same result each time.每次都得到相同的结果。

Running environment : This is a production server running Apache2 with PHP 7.1 under Debian 9.运行环境:这是在Debian 9下运行Apache2和PHP 7.1的生产服务器。

EDIT1 : I've made the same test on a fresh Laravel 5.7 install, same issue : EDIT1 :我对全新的 Laravel 5.7 安装进行了相同的测试,同样的问题:

Route::get('/', function () {
return Storage::disk('public')->download('import.csv', 'import.csv', ['Content-Type: text/csv"','Content-disposition: attachment;filename=MyVerySpecial.csv']);});

EDIT2 : I've added a new test with a controller. EDIT2 :我添加了一个带有控制器的新测试。 Route :路线 :

Route::get('test', 'test@test');

Controller test :控制器test

public function test()
{
  return Storage::disk('public')->download('import.csv', 'import.csv', ['Content-Type: text/csv"','Content-disposition: attachment;filename=MyVerySpecial.csv']);
}

Exactly the same result完全一样的结果

EDIT3 : Tested with headers: EDIT3 :用标题测试:

['Content-Type: application/octet-stream"','Content-disposition: attachment;filename="MyVerySpecial.csv"']

Same result结果相同

EDIT4 : Looks to be a server configuration problem as I have this issue with Firefox (last developer edition) and not with IE EDIT4 :看起来是服务器配置问题,因为我在 Firefox(最后一个开发者版本)而不是 IE 上有这个问题

Thank you for your help.感谢您的帮助。

EDIT5 : I think there is something wrong with Laravel because if I'm creating a php file with: EDIT5 :我认为Laravel有问题,因为如果我正在创建一个 php 文件:

<?php
$file = "../storage/app/public/import.csv";
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=" . basename($file));
readfile ($file);
exit();    

This is working perfectly with Firefox.这与 Firefox 完美配合。 But if I use a route like但是如果我使用像这样的路线

return Storage::disk('public')->download('import.csv', 'import.csv', ['Content-Description: File Transfer','Content-Type: application/octet-stream','Content-Disposition: attachment; filename=/home/webadmin/fresh/storage/app/public/import.csv']);

Well it does not work.那么它不起作用。

Question : has somebody else tried to reproduce same issue?问题:有没有其他人试图重现同样的问题?

The answer of this problem is format used for headers :这个问题的答案是用于标题的格式:

Route::get('/', function () {
    return Storage::disk('public')->download('import.csv', 'import.csv', ['Content-Description' =>  'File Transfer','Content-Type' => 'application/octet-stream','Content-Disposition' => 'attachment; filename=import.csv']);
});

This is a good way how to send headers to download files, tested on Firefox and IE and Chrome这是如何发送标头以下载文件的好方法,已在 Firefox、IE 和 Chrome 上测试

You have to add all these headers :您必须添加所有这些标题:

header('Content-type: application/octet-stream');
header('Content-Length: ' . filesize($data));
header('Content-Disposition: attachment; filename="export.csv"');

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

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