回答編集履歴

1

調整

2019/11/15 09:55

投稿

yambejp
yambejp

スコア116694

test CHANGED
@@ -11,3 +11,69 @@
11
11
  </div>
12
12
 
13
13
  ```
14
+
15
+
16
+
17
+ # 参考
18
+
19
+ ```javacscript
20
+
21
+ <script>
22
+
23
+ window.addEventListener('DOMContentLoaded', ()=>{
24
+
25
+ document.querySelector('#up').addEventListener('change',e=>{
26
+
27
+ var target=e.target;
28
+
29
+ var file=target.files[0];
30
+
31
+ if(file !==undefined){
32
+
33
+ var fr = new FileReader();
34
+
35
+ fr.addEventListener('load',e=>{
36
+
37
+ var type=file.type;
38
+
39
+ if(["image/jpeg","image/gif","image/png"].indexOf(type)>-1){
40
+
41
+ var blob=new Blob([e.target.result],{type});
42
+
43
+ var binary=String.fromCharCode.apply("",new Uint8Array(e.target.result));
44
+
45
+ var base64="data:"+file.type+";base64,"+btoa(binary);
46
+
47
+ var img=Object.assign(document.createElement('img'),{src:base64});
48
+
49
+ Object.assign(document.querySelector('#img'),{textContent:""}).appendChild(img);
50
+
51
+ }else{
52
+
53
+ target.value="";
54
+
55
+ document.querySelector('#img').textContent="";
56
+
57
+ }
58
+
59
+ });
60
+
61
+ fr.readAsArrayBuffer(file);
62
+
63
+ }
64
+
65
+ });
66
+
67
+ });
68
+
69
+ </script>
70
+
71
+ <input type="file" value="up" id="up" accept="image/*">
72
+
73
+ <div id="img">
74
+
75
+ <!--ここに画像をいれる-->
76
+
77
+ </div>
78
+
79
+ ```