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

質問編集履歴

1

追加

2016/02/09 09:59

投稿

dialbird
dialbird

スコア379

title CHANGED
File without changes
body CHANGED
@@ -5,4 +5,118 @@
5
5
 
6
6
  文字や信号は送れるのですが、画像データはどのようにすれば良いのかと‥‥
7
7
 
8
- よろしくお願いいたします。
8
+ よろしくお願いいたします。
9
+
10
+ 追記:
11
+ 回答者様のアドバイスから、base64に変換して送信するということがわかったので、inputのfileタイプでアップロードした画像をsocketで送るというのをやってみたのですが(以下のコード)、base64に変換してからsocketで送信することは出来ているものの、受信側でエラー([object%20Object] 404 (Not Found))
12
+ なるものが出て、画像になりません。どこが間違っているのでしょうか?よろしくお願いいたします
13
+
14
+ ```javascript
15
+ //app.js
16
+ var express = require('express'),
17
+ app = express(),
18
+ server = require('http').Server(app),
19
+ io = require('socket.io').listen(server),
20
+ ejs = require('ejs');
21
+
22
+
23
+ app.set('views',__dirname+'/views');
24
+ app.set('view engine', 'ejs');
25
+
26
+ app.use('/public',express.static(__dirname+'/public'));
27
+
28
+ app.get('/',function(req,res){
29
+ res.render('index');
30
+ })
31
+
32
+ app.get('/index2',function(req,res){
33
+ res.render('index2');
34
+ })
35
+
36
+
37
+ server.listen(3000);
38
+ console.log('now comming...');
39
+
40
+
41
+ io.sockets.on('connection',function(socket){
42
+ //画像をindex1からindex2へと渡す
43
+ socket.on('send_img',function(data){
44
+ console.log(data);
45
+ io.sockets.emit('send_img',data);
46
+ });
47
+
48
+ })
49
+ ```
50
+
51
+ ```html
52
+ //index1
53
+ <!DOCTYPE html>
54
+ <html lang="ja">
55
+ <head>
56
+ <meta charset="UTF-8">
57
+ <title>index</title>
58
+ </head>
59
+ <body>
60
+ <h1>画像を送る側</h1>
61
+ <input type="file" accept="image/*" id="image">
62
+ <button id="send">send</button>
63
+ <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
64
+ <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
65
+
66
+ <script>
67
+ $(function(){
68
+ //socket接続
69
+ var socket = io.connect();
70
+
71
+ //画像プレビュー
72
+ var image = $('#image').on('change',function(){
73
+ var fileList = $(this).prop('files');
74
+ var reader = new FileReader();
75
+ reader.readAsDataURL(fileList[0]);
76
+
77
+ reader.onload = function(){
78
+ $('<img width="200" class="preview">').attr('src',reader.result).appendTo('body');
79
+ }
80
+ })
81
+
82
+ //sendボタンを押して、index2へと画像送信
83
+ $('#send').on('click',function(){
84
+ var src = $('.preview').attr('src');
85
+ socket.emit('send_img',{
86
+ data: src
87
+ });
88
+ });
89
+
90
+ })
91
+ </script>
92
+ </body>
93
+ </html>
94
+ ```
95
+
96
+ ```html
97
+ //index2
98
+ <!DOCTYPE html>
99
+ <html lang="ja">
100
+ <head>
101
+ <meta charset="UTF-8">
102
+ <title>index2</title>
103
+ </head>
104
+ <body>
105
+ <h1>ここに画像が流れてくるよ</h1>
106
+ <div id="come_image"></div>
107
+ <script src="//code.jquery.com/jquery-1.12.0.min.js"></script>
108
+ <script src="https://cdn.socket.io/socket.io-1.3.5.js"></script>
109
+ <script>
110
+ $(function(){
111
+ //socket接続
112
+ var socket = io.connect();
113
+
114
+ socket.on('send_img',function(data){
115
+ console.log(data); //これは出てきました
116
+ $('<img width="200">').attr('src',data).appendTo('#come_image');//これが画像にならない
117
+ })
118
+ })
119
+ </script>
120
+ </body>
121
+ </html>
122
+ ```