回答編集履歴

2

調整

2019/09/12 10:40

投稿

yambejp
yambejp

スコア114839

test CHANGED
@@ -88,4 +88,10 @@
88
88
 
89
89
  });
90
90
 
91
+ </script>
92
+
93
+
94
+
91
95
  ```
96
+
97
+ ※IEを除く

1

chousei

2019/09/12 10:40

投稿

yambejp
yambejp

スコア114839

test CHANGED
@@ -25,3 +25,67 @@
25
25
  ```
26
26
 
27
27
  ヘッダ以外のデータをどうしたいのか読み取れないのであとは追加サンプル次第
28
+
29
+
30
+
31
+
32
+
33
+ # download
34
+
35
+
36
+
37
+ ファイルをダウンロードさせるにはhtml5のaタグdownload属性を使います
38
+
39
+ その際はPHP側も素直にheaderにcsvを指定します
40
+
41
+
42
+
43
+ - download.php
44
+
45
+ ```PHP
46
+
47
+ <?PHP
48
+
49
+ header('Content-Type: text/csv;charset=Shift_JIS');
50
+
51
+ header('Content-Disposition: attachment; filename=test.csv');
52
+
53
+ header('Content-Transfer-Encoding:8bit');
54
+
55
+
56
+
57
+ $csv_header =['id', 'name'];
58
+
59
+ print implode(",",$csv_header);
60
+
61
+ ```
62
+
63
+
64
+
65
+ - html
66
+
67
+ ```javascript
68
+
69
+ <script>
70
+
71
+ window.addEventListener('DOMContentLoaded', ()=>{
72
+
73
+ var url="download.php";
74
+
75
+ var a=document.createElement('a');
76
+
77
+ a.setAttribute('href',url);
78
+
79
+ a.setAttribute('download','test.csv');
80
+
81
+ a.setAttribute('style','display:block;position:absolute;top:-999px;');
82
+
83
+ document.querySelector('body').appendChild(a);
84
+
85
+ a.click();
86
+
87
+ a.parentNode.removeChild(a);
88
+
89
+ });
90
+
91
+ ```