动态生成下载(在我的例子中是 .txt 文件)的最佳方法是什么?我的意思是之前没有将文件存储在服务器上。为了理解这里是我需要完成的事情:
public function getDesktopDownload(Request $request){
$txt = "Logs ";
//offer the content of txt as a download (logs.txt)
$headers = ['Content-type' => 'text/plain', 'Content-Disposition' => sprintf('attachment; filename="test.txt"'), 'Content-Length' => sizeof($txt)];
return Response::make($txt, 200, $headers);
}
最佳答案
试试这个
public function getDownload(Request $request) {
// prepare content
$logs = Log::all();
$content = "Logs \n";
foreach ($logs as $log) {
$content .= $logs->id;
$content .= "\n";
}
// file name that will be used in the download
$fileName = "logs.txt";
// use headers in order to generate the download
$headers = [
'Content-type' => 'text/plain',
'Content-Disposition' => sprintf('attachment; filename="%s"', $fileName),
'Content-Length' => sizeof($content)
];
// make a response, with the content, a 200 response code and the headers
return Response::make($content, 200, $headers);
}
关于php - laravel5.3 即时生成下载(.txt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40084474/