user1725563
user1725563

Reputation: 25

how to emit a file through socket.io

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"

Upvotes: 1

Views: 3564

Answers (1)

Kaustubh Karkare
Kaustubh Karkare

Reputation: 1101

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

Upvotes: 1

Related Questions