how to emit a file through socket.io

浪子不回头ぞ 提交于 2021-01-28 10:23:51

问题


how can i send a file path through socket.io i.e

genrally we use

socket.emit("sendpath","hi" );
socket.on("sendpath",function()
{
console.log("hi")
}

to emit a file path i.e a pdf file which is in my c drive how can that happen

path is :-"C:\xampp\248.pdf"


回答1:


I'm not completely sure I understand your question, but I'm going to assume that you want the contents of a file, given the path to that file, provided as a Socket.IO event.

// var fs = require("fs");
socket.on("sendpath",filepath){
    fs.readFile(filepath,function(error, filedata){
        if(error) throw error;
        else socket.emit("sendfile", filedata.toString() );
    });
});

Reference: http://nodejs.org/api/fs.html#fs_fs_readfile_filename_encoding_callback



来源:https://stackoverflow.com/questions/12817173/how-to-emit-a-file-through-socket-io

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