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

回答編集履歴

2

chousei

2021/06/09 04:03

投稿

yambejp
yambejp

スコア117885

answer CHANGED
@@ -30,7 +30,7 @@
30
30
  const blob2 = new Blob([buffer.buffer], {type: content.type});
31
31
  fd.append(file.getAttribute('name')+"[]",blob2,content.name);
32
32
  });
33
- fetch(url,{method:"post",body:fd}).then(res=>res.text()).then(console.log);
33
+ fetch(url,{method:"post",body:fd}).then(res=>res.blob()).then(console.log);
34
34
  });
35
35
  fr.readAsArrayBuffer(blob);
36
36
  });

1

てすと

2021/06/09 04:03

投稿

yambejp
yambejp

スコア117885

answer CHANGED
@@ -3,4 +3,59 @@
3
3
  ネット回線およびサーバー側の処理能力の問題なので
4
4
  クライアント側でできることはほぼありません
5
5
  例えば分割して並行してアップロードするとしても回線がボトルネック
6
- になればアップロード時間は理論的にはさほど変わらないはずです
6
+ になればアップロード時間は理論的にはさほど変わらないはずです
7
+
8
+ # 分割してfetch
9
+ //send.html
10
+ ```javascript
11
+ <script>
12
+ window.addEventListener('DOMContentLoaded', ()=>{
13
+ document.querySelector('#btn').addEventListener('click',e=>{
14
+ e.preventDefault();
15
+ const file=document.querySelector('#file');
16
+ const url=file.form.getAttribute("action");
17
+ const content = file.files[0];
18
+ const blob=new Blob([content],{type:file.type});
19
+ const fr = new FileReader();
20
+ const sep_num=8;//好きなだけ分割してください
21
+ const fd=new FormData();
22
+ fr.addEventListener('load',e=>{
23
+ let src=e.target.result;
24
+ src=new Uint8Array(src);
25
+ src=String.fromCharCode.apply("",src);
26
+ const sep_size=parseInt(src.length/sep_num)+1;
27
+ new Array(sep_num).fill(null).forEach((_,x)=>{
28
+ const sep_file=src.substr(x*sep_size,sep_size);
29
+ const buffer = new Uint8Array(sep_file.length).map((_,x)=>sep_file.charCodeAt(x));
30
+ const blob2 = new Blob([buffer.buffer], {type: content.type});
31
+ fd.append(file.getAttribute('name')+"[]",blob2,content.name);
32
+ });
33
+ fetch(url,{method:"post",body:fd}).then(res=>res.text()).then(console.log);
34
+ });
35
+ fr.readAsArrayBuffer(blob);
36
+ });
37
+ });
38
+ </script>
39
+ <form action="recv.php" method="post" enctype="multipart/form-data">
40
+ <input type="file" id="file" name="myfile">
41
+ <input type="button" value="fetch" id="btn">
42
+ <input type="submit" value="send">
43
+ </form>
44
+ ```
45
+ // recv.php
46
+ ```PHP
47
+ <?PHP
48
+ if(count($_FILES)==0 or !isset($_FILES["myfile"])){
49
+ print "no data";
50
+ }elseif(count($_FILES["myfile"]["tmp_name"])==1){
51
+ header("Content-Type:".$_FILES["myfile"]["type"]);
52
+ readfile($_FILES["myfile"]["tmp_name"]);
53
+ exit;
54
+ }else{
55
+ header("Content-Type:".$_FILES["myfile"]["type"][0]);
56
+ foreach($_FILES["myfile"]["tmp_name"] as $val){
57
+ readfile($val);
58
+ }
59
+ exit;
60
+ }
61
+ ```