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

回答編集履歴

1

コード修正

2021/08/24 05:39

投稿

int32_t
int32_t

スコア21933

answer CHANGED
@@ -1,3 +1,17 @@
1
1
  ダウンロードするフォルダをウェブページから指定することはできません。
2
2
 
3
- Google Chrome など、[File System Access API](https://wicg.github.io/file-system-access/) をサポートしているブラウザであれば、[`showDirectoryPicker()`](https://wicg.github.io/file-system-access/#api-showdirectorypicker)でユーザにディレクトリを指定してもらって、[`getFileHandle('name.mid', {create:true})`](https://wicg.github.io/file-system-access/#dom-filesystemdirectoryhandle-getfilehandle) でファイルを作ることが可能だと思います。
3
+ Google Chrome など、[File System Access API](https://wicg.github.io/file-system-access/) をサポートしているブラウザであれば、[`showDirectoryPicker()`](https://wicg.github.io/file-system-access/#api-showdirectorypicker)で**ユーザにディレクトリを指定してもらって**、[`getFileHandle('name.mid', {create:true})`](https://wicg.github.io/file-system-access/#dom-filesystemdirectoryhandle-getfilehandle) でファイルを作ることが可能す。
4
+
5
+ (どっちにしても、ウェブページ側でファルダを指定することはセキュリティ上の理由で不可能です。)
6
+
7
+ 以下、サンプルコードです。https:// でのみ動きます。
8
+ ```js
9
+ button.addEventListener('click', async () => {
10
+ ...
11
+ let dirHandle = await window.showDirectoryPicker();
12
+ let fileHandle = await dirHandle.getFileHandle('name.midi', {create:true});
13
+ let stream = await fileHandle.createWritable();
14
+ await stream.write(new MidiWriter.Writer(track).buildFile());
15
+ await stream.close();
16
+ });
17
+ ```