Pipe not working

2.9k views Asked by At

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);
}  
2

There are 2 answers

2
mscdex On BEST ANSWER

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.

1
Arjun Shukla On

Try this:

var fs = require('fs');
var url = require('url');
var http = require('http');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var file_url = <download url>;
var DOWNLOAD_DIR = './downloads/';
var mkdir = 'mkdir -p ' + DOWNLOAD_DIR;
var child = exec(mkdir, function(err, stdout, stderr) {
    if (err) throw err;
    else {
        var file_name = url.parse(file_url).pathname.split('/').pop();
        var file = fs.createWriteStream(DOWNLOAD_DIR + file_name);
        var options =
        { method: 'GET',
          url: file_url,
          headers:
        {authorization: '<your auth>'}
        };
        request(options).pipe(file); /* This line does the trick */
        console.log(file_name + ' downloaded to ' + DOWNLOAD_DIR);
    }
});