我可以通过 node.js 中的 socket.io 将文件上传到服务器吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5973825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I upload a file to server by socket.io in node.js?
提问by XMen
I have a socket.io clientto whom socket.io server is listening in node.js. I want to upload a file to server by socket.io. because my socket.ioclient is under apache. and socket.ioserver is in Node.js. I want to code uploading of file in Node.js. But how I can send the file data to the node.js listening to socket.
我有一个socket.io 客户端,socket.io 服务器在 node.js 中监听它。我想将文件上传到服务器socket.io。因为我的socket.io客户在 apache 下。并且socket.io服务器在 Node.js 中。我想在 Node.js 中编写文件上传代码。但是我如何将文件数据发送到监听套接字的 node.js。
I have tried the upload of a file using Node.js server. This link is here but the this code does not use the socket.iowith this. But my original project is using the socket.io.
我已经尝试使用 Node.js 服务器上传文件。这个链接在这里但是这个代码不使用socket.io这个。但是我原来的项目是使用socket.io.
Uploading a file in node.js by formidable
How can I do this with the socket.ioserver?
我怎样才能用socket.io服务器做到这一点?
回答by Olivier Refalo
The checked answer is invalid as of Jul 3rd 2012.
已检查的答案自 2012 年 7 月 3 日起无效。
Read by yourself this tutorial on NetTuts. This tutorial uses node.js and socket.io to upload a file.
在 NetTuts 上自行阅读本教程。本教程使用 node.js 和 socket.io 上传文件。
回答by nkzawa
回答by sffc
Here is a npm module I wrote that will implement Socket.IO file uploading in your application:
这是我编写的一个 npm 模块,它将在您的应用程序中实现 Socket.IO 文件上传:
All you need to give the module is your middleware, your socket, and the file input or drop zone used for uploading files. It will also save the files if you give it a directory. The module takes care of the rest.
您需要为模块提供的只是您的中间件、套接字以及用于上传文件的文件输入或拖放区。如果你给它一个目录,它也会保存文件。该模块负责其余的工作。
回答by Rsh
In May of 2014 with release version 1.0 of socket.IO, binary support has been added to the library.
2014 年 5 月,随着 socket.IO 1.0 版的发布,库中添加了二进制支持。
Socket.IO now supports emitting Buffer (from Node.JS), Blob, ArrayBuffer and even File, as part of any datastructure
Socket.IO 现在支持发送 Buffer(来自 Node.JS)、Blob、ArrayBuffer 甚至 File,作为任何数据结构的一部分
It's all due to low level api which is provided by engine.io.
这都是由于engine.io提供的低级api。
回答by studgeek
Here is an example of sending a file over socket.io: http://liamkaufman.com/blog/2012/02/11/12-pushing-files-to-the-browser-using-deliveryjs-socketio-and-nodejs/
这是通过 socket.io 发送文件的示例:http: //liamkaufman.com/blog/2012/02/11/12-pushing-files-to-the-browser-using-deliveryjs-socketio-and-nodejs /

