回答編集履歴
2
ちょうせい
test
CHANGED
@@ -53,6 +53,8 @@
|
|
53
53
|
const list=[];
|
54
54
|
|
55
55
|
document.querySelector('.droparea').addEventListener('change',e=>{
|
56
|
+
|
57
|
+
list.length=0;
|
56
58
|
|
57
59
|
var target=e.target;
|
58
60
|
|
1
chousei
test
CHANGED
@@ -1,5 +1,149 @@
|
|
1
|
-
|
1
|
+
こんな感じで
|
2
2
|
|
3
|
-
|
3
|
+
```javascript
|
4
4
|
|
5
|
+
<?PHP
|
6
|
+
|
7
|
+
print_r($_FILES);//自分自身に送ってファイルを確認する
|
8
|
+
|
9
|
+
?>
|
10
|
+
|
11
|
+
<style>
|
12
|
+
|
13
|
+
.droppable{
|
14
|
+
|
15
|
+
border:1px solid;
|
16
|
+
|
17
|
+
position:relative;
|
18
|
+
|
19
|
+
height:300px;
|
20
|
+
|
21
|
+
}
|
22
|
+
|
23
|
+
.droparea{
|
24
|
+
|
25
|
+
position:absolute;
|
26
|
+
|
27
|
+
top:0;
|
28
|
+
|
29
|
+
width:100%;
|
30
|
+
|
31
|
+
height:100%;;
|
32
|
+
|
33
|
+
opacity:0;
|
34
|
+
|
35
|
+
background-Color:lime;
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
#preview img{
|
40
|
+
|
41
|
+
max-width:100px;
|
42
|
+
|
43
|
+
max-height:100px;
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
</style>
|
48
|
+
|
49
|
+
<script>
|
50
|
+
|
51
|
+
window.addEventListener('DOMContentLoaded', ()=>{
|
52
|
+
|
53
|
+
const list=[];
|
54
|
+
|
55
|
+
document.querySelector('.droparea').addEventListener('change',e=>{
|
56
|
+
|
57
|
+
var target=e.target;
|
58
|
+
|
59
|
+
document.querySelector('#preview').textContent="";
|
60
|
+
|
61
|
+
[...target.files].forEach((file,idx)=>{
|
62
|
+
|
63
|
+
if(file !==undefined){
|
64
|
+
|
65
|
+
var fr = new FileReader();
|
66
|
+
|
67
|
+
fr.addEventListener('load',e=>{
|
68
|
+
|
69
|
+
var type=file.type;
|
70
|
+
|
71
|
+
var name=file.name;
|
72
|
+
|
73
|
+
if(["image/jpeg","image/gif","image/png"].includes(type)){
|
74
|
+
|
75
|
+
var blob=new Blob([e.target.result],{type});
|
76
|
+
|
77
|
+
var binary=String.fromCharCode.apply("",new Uint8Array(e.target.result));
|
78
|
+
|
79
|
+
var base64="data:"+file.type+";base64,"+btoa(binary);
|
80
|
+
|
81
|
+
var img=Object.assign(document.createElement('img'),{src:base64});
|
82
|
+
|
83
|
+
list.push({blob,name});
|
84
|
+
|
85
|
+
img.addEventListener('click',()=>{
|
86
|
+
|
87
|
+
list[idx]=false;
|
88
|
+
|
89
|
+
console.log(list);
|
90
|
+
|
91
|
+
img.remove();
|
92
|
+
|
93
|
+
});
|
94
|
+
|
95
|
+
document.querySelector('#preview').appendChild(img);
|
96
|
+
|
97
|
+
}else{
|
98
|
+
|
99
|
+
target.value="";
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
});
|
104
|
+
|
105
|
+
fr.readAsArrayBuffer(file);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
});
|
110
|
+
|
111
|
+
});
|
112
|
+
|
113
|
+
document.addEventListener('submit',(e)=>{
|
114
|
+
|
115
|
+
document.querySelector('[type=file]').disabled=true;
|
116
|
+
|
117
|
+
});
|
118
|
+
|
119
|
+
document.addEventListener('formdata',(e)=>{
|
120
|
+
|
121
|
+
list.forEach(x=>{
|
122
|
+
|
123
|
+
if(x) e.formData.append(document.querySelector('[type=file]').name,x.blob,x.name);
|
124
|
+
|
125
|
+
});
|
126
|
+
|
127
|
+
});
|
128
|
+
|
129
|
+
});
|
130
|
+
|
131
|
+
</script>
|
132
|
+
|
133
|
+
<form method="post" enctype="multipart/form-data">
|
134
|
+
|
5
|
-
|
135
|
+
<div class="droppable">
|
136
|
+
|
137
|
+
<div>ドロップして</div>
|
138
|
+
|
139
|
+
<input type="file" class="droparea" name="myfile[]" multiple>
|
140
|
+
|
141
|
+
</div>
|
142
|
+
|
143
|
+
<input type="submit">
|
144
|
+
|
145
|
+
<div id="preview"></div>
|
146
|
+
|
147
|
+
</form>
|
148
|
+
|
149
|
+
```
|