質問編集履歴

1

追加

2016/02/09 09:59

投稿

dialbird
dialbird

スコア379

test CHANGED
File without changes
test CHANGED
@@ -13,3 +13,231 @@
13
13
 
14
14
 
15
15
  よろしくお願いいたします。
16
+
17
+
18
+
19
+ 追記:
20
+
21
+ 回答者様のアドバイスから、base64に変換して送信するということがわかったので、inputのfileタイプでアップロードした画像をsocketで送るというのをやってみたのですが(以下のコード)、base64に変換してからsocketで送信することは出来ているものの、受信側でエラー([object%20Object] 404 (Not Found))
22
+
23
+ なるものが出て、画像になりません。どこが間違っているのでしょうか?よろしくお願いいたします
24
+
25
+
26
+
27
+ ```javascript
28
+
29
+ //app.js
30
+
31
+ var express = require('express'),
32
+
33
+ app = express(),
34
+
35
+ server = require('http').Server(app),
36
+
37
+ io = require('socket.io').listen(server),
38
+
39
+ ejs = require('ejs');
40
+
41
+
42
+
43
+
44
+
45
+ app.set('views',__dirname+'/views');
46
+
47
+ app.set('view engine', 'ejs');
48
+
49
+
50
+
51
+ app.use('/public',express.static(__dirname+'/public'));
52
+
53
+
54
+
55
+ app.get('/',function(req,res){
56
+
57
+ res.render('index');
58
+
59
+ })
60
+
61
+
62
+
63
+ app.get('/index2',function(req,res){
64
+
65
+ res.render('index2');
66
+
67
+ })
68
+
69
+
70
+
71
+
72
+
73
+ server.listen(3000);
74
+
75
+ console.log('now comming...');
76
+
77
+
78
+
79
+
80
+
81
+ io.sockets.on('connection',function(socket){
82
+
83
+ //画像をindex1からindex2へと渡す
84
+
85
+ socket.on('send_img',function(data){
86
+
87
+ console.log(data);
88
+
89
+ io.sockets.emit('send_img',data);
90
+
91
+ });
92
+
93
+
94
+
95
+ })
96
+
97
+ ```
98
+
99
+
100
+
101
+ ```html
102
+
103
+ //index1
104
+
105
+ <!DOCTYPE html>
106
+
107
+ <html lang="ja">
108
+
109
+ <head>
110
+
111
+ <meta charset="UTF-8">
112
+
113
+ <title>index</title>
114
+
115
+ </head>
116
+
117
+ <body>
118
+
119
+ <h1>画像を送る側</h1>
120
+
121
+ <input type="file" accept="image/*" id="image">
122
+
123
+ <button id="send">send</button>
124
+
125
+ <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
126
+
127
+ <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
128
+
129
+
130
+
131
+ <script>
132
+
133
+ $(function(){
134
+
135
+ //socket接続
136
+
137
+ var socket = io.connect();
138
+
139
+
140
+
141
+ //画像プレビュー
142
+
143
+ var image = $('#image').on('change',function(){
144
+
145
+ var fileList = $(this).prop('files');
146
+
147
+ var reader = new FileReader();
148
+
149
+ reader.readAsDataURL(fileList[0]);
150
+
151
+
152
+
153
+ reader.onload = function(){
154
+
155
+ $('<img width="200" class="preview">').attr('src',reader.result).appendTo('body');
156
+
157
+ }
158
+
159
+ })
160
+
161
+
162
+
163
+ //sendボタンを押して、index2へと画像送信
164
+
165
+ $('#send').on('click',function(){
166
+
167
+ var src = $('.preview').attr('src');
168
+
169
+ socket.emit('send_img',{
170
+
171
+ data: src
172
+
173
+ });
174
+
175
+ });
176
+
177
+
178
+
179
+ })
180
+
181
+ </script>
182
+
183
+ </body>
184
+
185
+ </html>
186
+
187
+ ```
188
+
189
+
190
+
191
+ ```html
192
+
193
+ //index2
194
+
195
+ <!DOCTYPE html>
196
+
197
+ <html lang="ja">
198
+
199
+ <head>
200
+
201
+ <meta charset="UTF-8">
202
+
203
+ <title>index2</title>
204
+
205
+ </head>
206
+
207
+ <body>
208
+
209
+ <h1>ここに画像が流れてくるよ</h1>
210
+
211
+ <div id="come_image"></div>
212
+
213
+ <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
214
+
215
+ <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
216
+
217
+ <script>
218
+
219
+ $(function(){
220
+
221
+ //socket接続
222
+
223
+ var socket = io.connect();
224
+
225
+
226
+
227
+ socket.on('send_img',function(data){
228
+
229
+ console.log(data); //これは出てきました
230
+
231
+ $('<img width="200">').attr('src',data).appendTo('#come_image');//これが画像にならない
232
+
233
+ })
234
+
235
+ })
236
+
237
+ </script>
238
+
239
+ </body>
240
+
241
+ </html>
242
+
243
+ ```