1

I need to be able to retrieve a PDF document from a URL and pass it to another function which will eventually store it in a database. I have found many solutions for DOWNLOADING files to local storage but I do not want to do this. The simplest method to grab the file that I found looks like this:

var http = require('http');
var fs = require('fs');

var download = function(url, dest, cb) {
  var file = fs.createWriteStream(dest);
  var request = http.get(url, function(response) {
    response.pipe(file);
    file.on('finish', function() {
      file.close(cb);
    });
  });
}  

How do I take the information from the request and stick it into a constant so i can then use it in another function?

Thanks

1 Answer 1

1

If you use response.pipe(file) you are writing to a writable stream, in this case a file. As you don't want to use local storage, you should choose a different approach.

You could read the raw data chunks and combine them into a Buffer object as described here: https://stackoverflow.com/a/21024737/7821823. You can then pass the buffer to another function.

http.get(url), function(res) {
    var data = [];

    res.on('data', function(chunk) {
        data.push(chunk);
    }).on('end', function() {
        //at this point data is an array of Buffers
        //so Buffer.concat() can make us a new Buffer
        //of all of them together
        var buffer = Buffer.concat(data);
        console.log(buffer.toString('base64'));
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.