回答編集履歴

2

chousei

2021/06/09 04:03

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -62,7 +62,7 @@
62
62
 
63
63
  });
64
64
 
65
- fetch(url,{method:"post",body:fd}).then(res=>res.text()).then(console.log);
65
+ fetch(url,{method:"post",body:fd}).then(res=>res.blob()).then(console.log);
66
66
 
67
67
  });
68
68
 

1

てすと

2021/06/09 04:03

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -9,3 +9,113 @@
9
9
  例えば分割して並行してアップロードするとしても回線がボトルネック
10
10
 
11
11
  になればアップロード時間は理論的にはさほど変わらないはずです
12
+
13
+
14
+
15
+ # 分割してfetch
16
+
17
+ //send.html
18
+
19
+ ```javascript
20
+
21
+ <script>
22
+
23
+ window.addEventListener('DOMContentLoaded', ()=>{
24
+
25
+ document.querySelector('#btn').addEventListener('click',e=>{
26
+
27
+ e.preventDefault();
28
+
29
+ const file=document.querySelector('#file');
30
+
31
+ const url=file.form.getAttribute("action");
32
+
33
+ const content = file.files[0];
34
+
35
+ const blob=new Blob([content],{type:file.type});
36
+
37
+ const fr = new FileReader();
38
+
39
+ const sep_num=8;//好きなだけ分割してください
40
+
41
+ const fd=new FormData();
42
+
43
+ fr.addEventListener('load',e=>{
44
+
45
+ let src=e.target.result;
46
+
47
+ src=new Uint8Array(src);
48
+
49
+ src=String.fromCharCode.apply("",src);
50
+
51
+ const sep_size=parseInt(src.length/sep_num)+1;
52
+
53
+ new Array(sep_num).fill(null).forEach((_,x)=>{
54
+
55
+ const sep_file=src.substr(x*sep_size,sep_size);
56
+
57
+ const buffer = new Uint8Array(sep_file.length).map((_,x)=>sep_file.charCodeAt(x));
58
+
59
+ const blob2 = new Blob([buffer.buffer], {type: content.type});
60
+
61
+ fd.append(file.getAttribute('name')+"[]",blob2,content.name);
62
+
63
+ });
64
+
65
+ fetch(url,{method:"post",body:fd}).then(res=>res.text()).then(console.log);
66
+
67
+ });
68
+
69
+ fr.readAsArrayBuffer(blob);
70
+
71
+ });
72
+
73
+ });
74
+
75
+ </script>
76
+
77
+ <form action="recv.php" method="post" enctype="multipart/form-data">
78
+
79
+ <input type="file" id="file" name="myfile">
80
+
81
+ <input type="button" value="fetch" id="btn">
82
+
83
+ <input type="submit" value="send">
84
+
85
+ </form>
86
+
87
+ ```
88
+
89
+ // recv.php
90
+
91
+ ```PHP
92
+
93
+ <?PHP
94
+
95
+ if(count($_FILES)==0 or !isset($_FILES["myfile"])){
96
+
97
+ print "no data";
98
+
99
+ }elseif(count($_FILES["myfile"]["tmp_name"])==1){
100
+
101
+ header("Content-Type:".$_FILES["myfile"]["type"]);
102
+
103
+ readfile($_FILES["myfile"]["tmp_name"]);
104
+
105
+ exit;
106
+
107
+ }else{
108
+
109
+ header("Content-Type:".$_FILES["myfile"]["type"][0]);
110
+
111
+ foreach($_FILES["myfile"]["tmp_name"] as $val){
112
+
113
+ readfile($val);
114
+
115
+ }
116
+
117
+ exit;
118
+
119
+ }
120
+
121
+ ```