In my development environment I have two servers. One sends and image to the other over a POST
http request.
Client server does this:
fs.readFile(rawFile.path,'binary',function (err, file){
restler.post("http://0.0.0.0:5000",{
data: file,
headers:{
"Content-Type": rawFile.type,
}
}).on('complete',function(data,response){
console.log(data);
res.send("file went through")
})
The server that recieves the request does this:
server.post('/',function(req,res,next){
fs.writeFileSync("test.png",req.body,"binary",function(err){
if(err) throw err;
res.send("OK")
})
})
If i send a small image it works fine. However, if i send a large image although the file is saved correctly only the first upper portion of the image is displayed. The rest is black. Image size is correct.
I guess it's just the first chunk of the image that's being written on the file.
I've tried creating a readStream
and a writeStream
but it doesn't seem to work:
req.body.pipe(fs.createWriteStream('test.png'))
Can i stream directly from the binary data and pipe
it into the file? For what i've seen, readStream
is often used to stream from files not raw binary data.
I read a few posts but it doesn't seem to work for me.
I'm using restler
module in the client server and restify
in the other.
Thanks!