Node.js 如何使用node和socket.io将文件上载到服务器
好的,我得到了一个Node.js 如何使用node和socket.io将文件上载到服务器,node.js,sockets,fs,Node.js,Sockets,Fs,好的,我得到了一个html,我正试图将所选文件发送到服务器,以便保存 但目前我所能做的就是得到一个socket.io。对不起,只是校对很重要。socket.io。对不起,只是校对很重要。 $("#imgUpload").on('change',function(){ var selectedFile = this.files[0]; console.log(selectedFile); // Send message to chat a
html
,我正试图将所选文件发送到服务器,以便保存
但目前我所能做的就是得到一个
socket.io
。对不起,只是校对很重要。socket.io
。对不起,只是校对很重要。
$("#imgUpload").on('change',function(){
var selectedFile = this.files[0];
console.log(selectedFile);
// Send message to chat and save to mongo:
socket.emit('clientMessage', {
message: $('#chatMessage').val(),
name: user.name,
userId: user.userId,
image: selectedFile
});
// selectedFile.convertToBase64(function(base64){
// base64Img = base64;
// })
});
// When user sends message, emit it back to others
socket.on('clientMessage', function(data) {
console.log(chatId);
// Send the message to database and emit back to other users
createMessage(data, chatId, function() {
console.log(data.image); // This prints out a buffer
// This is suppose to write the file, BUT IT CAN'T WRITE A BUFFER
// fs.writeFile("/public/images/", data.image, function(err) {
// if(err) {
// return console.log(err);
// }
// console.log("The file was saved!");
// });
fs.writeFile("imageConversion.jpg", new Buffer(data.image, "base64"), function(err) {
if(err){
console.log("Error: ", err);
return;
}
console.log("image converted and saved to dir");
});