0

I want to download a file remotely but when I try to write it in the disk it's empty.

I tried this solution https://stackoverflow.com/a/12751657 with no success.

in my app (downloader)

function download(url, filename, callback){
    request.head(url, function(err, res, body){
        if(err){
            alert("An error occured while downloading data remotely.");
        }else{
            request(url).pipe(fs.createWriteStream(filename)).on('close', callback);
        }
    });
}

download('http://localhost:3000/db/user', user.db, function(){
   alert("Success");
});

In my remote server

app.get('/db/user', function(req, res){
    res.sendFile('data/user.txt', {root: __dirname});
});

but when i check user.db it's empty.

Community
  • 1
  • 1
sse
  • 47
  • 1
  • 10
  • writing is an issue here i guess check this out http://stackoverflow.com/a/2497040/1653870 also may be handle the request error event separatey Try debugging using `debugger` may be – Akarsh Satija Feb 05 '17 at 19:23
  • yeah. when i track the file. for few milliseconds it writes the data, after the operation is finish i open file again it's empty. – sse Feb 05 '17 at 19:24

0 Answers0