0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Laravel】文字列を直接CSVファイルでレスポンスする(動的に生成した文字列を直接ファイル形式でレスポンスする。)

Last updated at Posted at 2021-01-19

自分用のメモとして残します。

文字列をダウンロードファイルとしてレスポンスを返す方法をメモ。
→つまりレスポンス用にわざわざファイルを保存しない。

■やり方

\Responseクラスでレスポンス用のオブジェクトを明示的に作成する。

$content = 'hoge,hoge,hoge';
$headers = [
    'Content-type'        => 'text/plain; charset=utf-8',
    'Content-Disposition' => 'attachment; filename="download.txt"',
];
$response = \Response::make($content, 200, $headers);

return $response;
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?