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

回答編集履歴

4

コメント

2017/03/06 07:36

投稿

yambejp
yambejp

スコア118410

answer CHANGED
@@ -37,14 +37,14 @@
37
37
  });
38
38
  }
39
39
  });
40
- setTimeout(function(){console.log(fd)},3000);
40
+ // setTimeout(function(){console.log(fd)},3000);//デバグ用
41
41
  Promise.all( prm ).then(function(){
42
42
  console.log(fd);
43
43
  $.ajax({
44
44
  "url":"recv.php",
45
45
  "type":"post",
46
46
  "data":fd,
47
- // "dataType":"json",
47
+ // "dataType":"json",//とりあえずjson以外で送ってうまくいったらjsonにする
48
48
  "processData": false,
49
49
  "contentType": false,
50
50
  "success":function(data){

3

修正

2017/03/06 07:36

投稿

yambejp
yambejp

スコア118410

answer CHANGED
@@ -61,6 +61,7 @@
61
61
  ```
62
62
  ```HTML
63
63
  <form method="post" enctype="multipart/form-data">
64
+ <input type="text" name="comment"><br>
64
65
  <input type="file" name="img1"><br>
65
66
  <input type="file" name="img2"><br>
66
67
  <input type="file" name="img3"><br>

2

追記

2017/03/06 07:34

投稿

yambejp
yambejp

スコア118410

answer CHANGED
@@ -20,6 +20,9 @@
20
20
  var me=[];
21
21
  var file=[];
22
22
  var fd = new FormData();
23
+ $(this).parents('form').find('input[type=text]').each(function(i){
24
+ fd.append($(this).attr('name'),$(this).val());
25
+ });
23
26
  $(this).parents('form').find('input[type=file]').each(function(i){
24
27
  me[i] = $(this);
25
28
  file[i]=$(this).prop('files')[0];

1

追記

2017/03/06 07:33

投稿

yambejp
yambejp

スコア118410

answer CHANGED
@@ -8,4 +8,67 @@
8
8
  console.log(error)
9
9
  }
10
10
 
11
- で状況確認ください
11
+ で状況確認ください
12
+
13
+ # 追記
14
+
15
+ ```javascript
16
+ $(function() {
17
+ $('input[type=submit][value=upload]').on('click',function(e){
18
+ e.preventDefault();
19
+ var prm=[];
20
+ var me=[];
21
+ var file=[];
22
+ var fd = new FormData();
23
+ $(this).parents('form').find('input[type=file]').each(function(i){
24
+ me[i] = $(this);
25
+ file[i]=$(this).prop('files')[0];
26
+ if(file[i]!==undefined){
27
+ prm[i]= new Promise(function(resolver){
28
+ var fr = new FileReader();
29
+ fr.addEventListener("load", function(e){
30
+ fd.append(me[i].attr('name'), new Blob([e.target.result],{"type":file[i].type}),file[i].name );
31
+ resolver(this);
32
+ });
33
+ fr.readAsArrayBuffer(file[i]);
34
+ });
35
+ }
36
+ });
37
+ setTimeout(function(){console.log(fd)},3000);
38
+ Promise.all( prm ).then(function(){
39
+ console.log(fd);
40
+ $.ajax({
41
+ "url":"recv.php",
42
+ "type":"post",
43
+ "data":fd,
44
+ // "dataType":"json",
45
+ "processData": false,
46
+ "contentType": false,
47
+ "success":function(data){
48
+ console.log(data);
49
+ },
50
+ "error":function(xhr,error){
51
+ console.log(error);
52
+ },
53
+ });
54
+ });
55
+ });
56
+ });
57
+ </script>
58
+ ```
59
+ ```HTML
60
+ <form method="post" enctype="multipart/form-data">
61
+ <input type="file" name="img1"><br>
62
+ <input type="file" name="img2"><br>
63
+ <input type="file" name="img3"><br>
64
+ <input type="submit" value="upload">
65
+ </form>
66
+ ```
67
+
68
+ 受け側テスト
69
+ ```PHP
70
+ <?PHP
71
+ print_R($_POST);
72
+ print_R($_FILES);
73
+ ?>
74
+ ```