我的数据存储在数据库中,我需要在文本文件中给出它。在磁盘上创建文件是没有意义的,但是尝试用文件生成响应是不起作用的。找到了这样的方法,但它不起作用ERR_INVALID_RESPONSE
$contents = 'My data from db';
$filename = 'example.txt';
return response()->streamDownload(function () use ($contents) {
echo $contents;
}, $filename);
在您的路线名称中,包含合理内容的路线,如:
Route::get('filename.txt', [FooController::class, 'foo']);
在foo((操作的控制器中:
$headers = [
'Content-Type' => 'application/plain',
'Content-Description' => 'File name',
];
$contents = 'My data from db';
return Response::make($contents, 200, $headers);