I adapted the official Socket.io chat example and added the functionality of sending files / images and even video via base64 encoding, you can see the source code in client.js and index.js , the most important part below, I hope that it will be useful for you.
On the client side:
$('#uploadfile').bind('change', function(e){ var data = e.originalEvent.target.files[0]; readThenSendFile(data); }); function readThenSendFile(data){ var reader = new FileReader(); reader.onload = function(evt){ var msg ={}; msg.username = username; msg.file = evt.target.result; msg.fileName = data.name; socket.emit('base64 file', msg); }; reader.readAsDataURL(data); }
On the server side:
socket.on('base64 file', function (msg) { console.log('received base64 file from' + msg.username); socket.username = msg.username;
Here's the project:
https://github.com/Arch1tect/Chatbox
source share