I have a server side script written in Node.js and client side script written in C#. I'm trying to save file uploading from C# script using pipe method. However, I get zero byte file. This is what I have done -
restify = require('restify'),
session = require('restify-session')({
debug : false,
ttl : 1200 // Time to live in seconds
});
request = require('request');
fs = require('fs');
server = restify.createServer();
server.use(restify.queryParser());
server.use(restify.bodyParser());
server.post('/upload', uploadFile);
function uploadFile(req, res, next) {
var x = fs.createWriteStream('file.jpg');
req.pipe(x);
}
I checked the req.body and seems that it contains content of the file C# script is trying to upload. So I don't think there is a problem with C# script. It is preceded by the query string though.
Thanks
UPDATE
I removed the body parser and now pipe is working. However, it's also writing some header information such as Content-Disposition, Content-Type along with file content. Do I need to user specific parser?
restify = require('restify'),
session = require('restify-session')({
debug : false,
ttl : 1200 // Time to live in seconds
});
request = require('request');
fs = require('fs');
server = restify.createServer();
server.post('/upload', restify.queryParser(), uploadFile);
function uploadFile(req, res, next) {
var x = fs.createWriteStream('file.jpg');
req.pipe(x);
}
If you see
req.body
set to something, that means the request has already been processed, so there is nothing to pipe to your file WriteStream.So you need to either move your body parsing middleware so that they are only executed for routes that need them, or (as more of a hack) you could write a custom middleware that only dynamically calls the body parsing middleware if it doesn't meet the criteria for the route where you are wanting to upload the file.