-1

I'm trying to download an image and then upload the image to Twitter. When I try to download the image it won't download until the app has finished which means I can't upload it to Twitter.

I believe the node.js is asynchronous so this is causing the issue. Any suggestions would be great.

Below is my download function.

// downloads the image
function download(url){
        var request = require('request'), 
            fs      = require('fs'),
            url     = url;
        count = count + 1; 
        request(url, {encoding: 'binary'}, function(error, response, body) {

         fs.writeFile('downloaded'+ count + '.jpg', body, 'binary', function (err) {

          });

        });


    }

I've tried using writeFileSync but that doesn't seem to work.

Derek MC
  • 366
  • 2
  • 4
  • 25
  • 1
    I see the code to download the file to your local hard drive. Where's the code to upload it somewhere else? You would have to put that inside your `fs.writeFile()` completion handler. FYI, you can also use streams to pipe from one input to another output. Hugely useful in cases like this. – jfriend00 Oct 22 '17 at 16:03
  • Thanks, yeah the function stores the image in the main folder on my local hard drive. I'll look into streams. Thanks – Derek MC Oct 22 '17 at 16:10
  • If you show us the code for uploading, we can help you with the streams part. – jfriend00 Oct 22 '17 at 16:15

1 Answers1

1

Update Checkout this answer if you wish to download and upload in one step.

Original Answer

Check out this answer for uploading.

And try this (for downloading):

var fs = require('fs'),
    request = require('request');

request({uri: url})
      .pipe(fs.createWriteStream(path))
      .on('close', function() {
        // done
      });

Taken from here

If you wish, you should actually be able to skip your drive and upload it as you're downloading it using streams as well.

I suggest you use promises for your async operations. They make writing async code a breeze and look a lot cleaner than callbacks. It's even better when you use promises with the async/await syntax (which is now natively supported in Node 8).

And here's a crash course on promises and async/await. I am sure you can find out more by Googling.

earthling
  • 555
  • 4
  • 14
  • This doesn't download and upload which is what the question asks. The OP's code already downloads appropriately. – jfriend00 Oct 22 '17 at 16:33
  • There's already a stackoverflow answer for uploading which I have linked to. No point in repeating. Just mix the two. – earthling Oct 22 '17 at 16:45
  • The ideal answer here would be to pipe from download to upload directly with no intervening local save. That is not covered in your answer or the one you reference. That's what this question is really about. If the OP would show us their upload code, we could all help in a more specific way and not generic answers. – jfriend00 Oct 22 '17 at 16:48
  • I've added an update with a link to another SO thread that has download/upload in one go. Sometimes people just have to google a bit. – earthling Oct 22 '17 at 16:56
  • That is a better reference and happens to be a good question/answer to mark this question a duplicate of. As you said, no point in repeating here and that is the stack overflow way (to mark a dup rather than repeating). – jfriend00 Oct 22 '17 at 16:59
  • Sorry, I've edited the question. I am looking to download the file and then upload it to Twitter(I'm creating a Twitter bot). So I'd need to download the image and then have the rest of the program run(eg load image and text to twitter). My issue is that the image only downloads after the full program has run. – Derek MC Oct 22 '17 at 17:24
  • The download is not complete until the callback is called so make sure the rest of your code is triggered in the callback. To offer more help, we really need to see more of your code. – earthling Oct 22 '17 at 17:33