回答編集履歴

1

調整

2022/05/24 03:56

投稿

yambejp
yambejp

スコア114829

test CHANGED
@@ -2,3 +2,21 @@
2
2
  type=fileで設定したデータを送る場合と
3
3
  html上で任意に作成したデータを送る場合は処理がことなります
4
4
  とくに後者の場合ファイル名の設定やマイムタイプの指定などが必要となります
5
+
6
+ # 追記
7
+ JSONデータをファイルで送りたいのかと思ったのですが
8
+ 質問の意図と違いましたかね・・・
9
+
10
+ ```javascript
11
+ const data={hoge:[1,2,3],fuga:["a","b","c"]};
12
+ const filename="test.json";
13
+ const json = JSON.stringify(data);
14
+ const type = "application/json";
15
+ const blob=new Blob([json],{type});
16
+ const method="post";
17
+ const body=new FormData();
18
+ body.append("test",blob,filename);
19
+ const url="・・・";
20
+ fetch(url,{method,body}).then(res=>res.text()).then(console.log);
21
+ ```
22
+ とすればファイルで送られます