Sending an image from client to server in socket.io

孤街醉人 提交于 2021-02-07 03:16:25

问题


Is it possible to upload image from client to server with socket.io?


回答1:


The WebSocket spec states that WebSockets should be able to send binary data as an ArrayBuffer, however this is not well supported by browsers. Socket.io limits you to sending strings, but you can send Base64 encoded data. So use the canvas toDataUrl() method to get an image as base64 and you're all set. Here's a great article on the subject.




回答2:


I would suggest using something like File Stack https://www.filestack.com/ The user on the client can upload an image to an s3 bucket and receive a url to the image in the callback. You can then send just the url to socket.io. I have actually implemented this for a project I am doing, where a performer can upload an image, and then share it with people in the room. Basically, they upload it via filepicker, get the url, send the url via socket.io and then socket.io on the server broadcasts it to everyone in the room.



来源:https://stackoverflow.com/questions/10154298/sending-an-image-from-client-to-server-in-socket-io

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!