質問するログイン新規登録

質問編集履歴

1

laravelのstorageに保存後、storageからpdfをdownloadさせるコード追記

2020/10/23 02:11

投稿

Koe
Koe

スコア0

title CHANGED
File without changes
body CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ### 該当のソースコード
12
12
 
13
- ```ここに言語名を入力
13
+ ```PHP
14
14
  public function generatePdf()
15
15
  {
16
16
  $data = 'some data' // bladeで使用する変数
@@ -29,7 +29,18 @@
29
29
  - ブラウザのキャッシュ削除
30
30
  - SSL導入(セキュリティの問題)
31
31
  - laravelのstorageに保存後、storageからpdfをdownloadさせる
32
+ ```PHP
33
+ $disk = 'local'; // or 's3'
34
+ $storage = Storage::disk($disk);
35
+ $file_name = 'my_file.pdf';
36
+ $pdf_path = 'pdf/' . $file_name;
37
+ $file = $storage->get($pdf_path);
38
+ return response($file, 200)
39
+ ->header('Content-Type', 'application/pdf')
40
+ ->header('Content-Type', 'application/force-download')
41
+ ->header('Content-Disposition', 'attachment; filename="' . $file_name . '"');
42
+ ```
32
- - header()の値を追加、変更
43
+ - header()の値を追加、変更(Content-length等)
33
44
 
34
45
  ### 補足情報(FW/ツールのバージョンなど)
35
46
  - Laravel Framework 6.17.1