Knob1
Knob1

Reputation: 53

Download File, save it and read it again --> Error

I would like to download a file, write it to a temporary file, read it and give the readFileSync Buffer to a function. I tried this:

var file = fs.createWriteStream("temp.pdf")
var request = http.get(linkArray[1], function(response) {
    response.on('data', function(data){
         file.write(data)
    }).on('end', function(){
         postData(fs.readFileSync('temp.pdf'))
    })
});

Sometimes it works, but sometimes it doesn't - my guess is that the file isn't written completely, when it is read. (But than the 'end' event shouldn't be fired ?! As you can see, I would like to download a bunch of files and do this. Do you have any advise how to solve this? Maybe this isn't the best way to solve this...

Upvotes: 1

Views: 4953

Answers (2)

generalhenry
generalhenry

Reputation: 17319

You shouldn't link streams with on('data' you should use pipe. Pipe will link the streams data events to writes and end events to ends.

var file = fs.createWriteStream("temp.pdf");
var request = http.get(linkArray[1], function(response) {
    response.pipe(file).on('close', function(){
         postData(fs.readFileSync('temp.pdf'));
    });
});

also you should use https://github.com/mikeal/request

var request = require('request');

request.get(linkArray[i], function (err, response, body) {
  postData(body);
});

or

var request = require('request');
var file = fs.createWriteStream("temp.pdf");

request.get(linkArray[i]).pipe(file).on('close', function () {
    postData(fs.readFileSync('temp.pdf'));   
});

Upvotes: 5

jmar777
jmar777

Reputation: 39679

You need to call file.end(); at the top of your .on('end', ...) handler. The end() method itself is asynchronous, though, so you'll want to read the file once that's complete. E.g.,

var file = fs.createWriteStream("temp.pdf")
var request = http.get(linkArray[1], function(response) {
    response.on('data', function(data){
        file.write(data)
    }).on('end', function(){
        file.end(function() {
            postData(fs.readFileSync('temp.pdf'))
        });
    })
});

Upvotes: 0

Related Questions