回答編集履歴

2

「Aタグ」→「A要素」

2019/10/26 18:27

投稿

matobaa
matobaa

スコア2493

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- [Blob](https://developer.mozilla.org/ja/docs/Web/API/Blob)に向けて[download属性](https://developer.mozilla.org/ja/docs/Web/HTML/Element/a#Attributes)を付与したaタグで実現できました。
5
+ [Blob](https://developer.mozilla.org/ja/docs/Web/API/Blob)に向けて[download属性](https://developer.mozilla.org/ja/docs/Web/HTML/Element/a#Attributes)を付与したa要素で実現できました。
6
6
 
7
7
 
8
8
 

1

ファイルの保存ダイアログを出すようにしてみました。

2019/10/26 18:27

投稿

matobaa
matobaa

スコア2493

test CHANGED
@@ -63,3 +63,51 @@
63
63
  </html>
64
64
 
65
65
  ```
66
+
67
+
68
+
69
+ # 追記
70
+
71
+
72
+
73
+ > ローカルPCの「ダウンロード」フォルダーを指定して保存する
74
+
75
+
76
+
77
+ ファイルの保存ダイアログを出すようにしてみました。
78
+
79
+ (chrome://flags のページで #native-file-system-api を有効にしてお試しください)
80
+
81
+
82
+
83
+ ```javascript:
84
+
85
+ // sign up origin trial; or enable the #native-file-system-api flag in chrome://flags.
86
+
87
+ // see: https://web.dev/native-file-system#write-file
88
+
89
+ async function writeFile(contents) {
90
+
91
+ const fileHandle = await window.chooseFileSystemEntries({ type: 'saveFile' });
92
+
93
+ const writer = await fileHandle.createWriter();
94
+
95
+ await writer.truncate(0);
96
+
97
+ await writer.write(0, contents);
98
+
99
+ await writer.close();
100
+
101
+ }
102
+
103
+ function download_and_save(src) {
104
+
105
+ fetch(src.attributes['data-href'].value)
106
+
107
+ .then(res => res.blob())
108
+
109
+ .then(writeFile)
110
+
111
+ }
112
+
113
+ ```