php - Laravel 5.7 存储下载 CSV 文件添加 .txt 扩展名
问题描述
在 Laravel 5.7 中,我使用了一个简单的路由来允许用户下载本地存储的 csv 文件。
Route::get('download/{file}', function($file) {
return Storage::disk('s3')->download($file, $file, ['Content-Type: text/csv"']);
})->name('download');
在我的 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>
当用户单击该链接时,Laravel 会在 s3 磁盘中找到 import.csv 文件并允许用户下载该文件,但它会添加一个 .txt 扩展名。所以用户正在下载 import.csv.txt 并且这个文件对应于 import.csv 数据。
如何防止 Laravel 向 CSV 文件添加 .txt 扩展名?我每次都尝试过return Storage::disk('s3')->download($file, $file);
,结果都是一样的。return Storage::disk('s3')->download($file);
运行环境:这是一个在 Debian 9 下运行 Apache2 和 PHP 7.1 的生产服务器。
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:我添加了一个带有控制器的新测试。路线 :
Route::get('test', 'test@test');
控制器test
:
public function test()
{
return Storage::disk('public')->download('import.csv', 'import.csv', ['Content-Type: text/csv"','Content-disposition: attachment;filename=MyVerySpecial.csv']);
}
结果完全一样
EDIT3:用标题测试:
['Content-Type: application/octet-stream"','Content-disposition: attachment;filename="MyVerySpecial.csv"']
结果相同
EDIT4:看起来是服务器配置问题,因为我在 Firefox(最后一个开发人员版本)而不是 IE 上遇到了这个问题
谢谢您的帮助。
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();
这与 Firefox 完美配合。但是如果我使用类似的路线
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']);
那么它不起作用。
问题:有没有其他人试图重现同样的问题?
解决方案
这个问题的答案是用于标题的格式:
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']);
});
这是发送标头下载文件的好方法,在 Firefox 和 IE 和 Chrome 上测试
推荐阅读
- python - 迭代贝叶斯优化包Python中的整数
- node.js - 在车把的默认布局中注入值
- tableau-api - 如何根据从 Tableau 中的快速过滤器中选择的内容显示先前的值
- c# - 对 API 端点的简单 PUT 请求
- python - 我们可以创建没有 self 的实例方法吗
- php - Codeigniter update_batch - 未定义的索引:id
- javascript - Promise.all 之后的返回值
- r - For 循环 & if else 适用于较少数据但不适用于更多数据
- c# - 如何使用 C# 中的 Auto Mapper 将嵌套的 JSON 映射到 Class 对象
- c++ - 如何将值初始化为向量中已清除的对象?