Stephen Berndt Stephen Berndt - 1 year ago 467
Node.js Question

node.js download images to server from list of http requests

I'm very new at node so bear with me. I'm trying to download a series of images from an external server. So far I have been able to get it working on a limited basis. When I run the below code, only about half of the images make it though to the web page. I know that I'm not doing this correctly, and am looking for some guidance. Here is the code that I have so far

var request = require("request"),
fs = require("fs"),
views = ['sitename1', 'sitename2', 'sitename3'...]
for (var view in views) {
url: 'http://' + SERVERURL + '/api/2.2/sites/' + siteID + '/workbooks/' + views[view]['workbookID'] + '/views/' + views[view]['id'] + '/previewimage',
headers: {
'Content-Type': 'image/png',
'X-Tableau-Auth': authToken
, encoding: 'binary'}).pipe(
fs.createWriteStream('./public/images/thumbnails/' + SITE + '/views/' + views[view]['url'] + '.png'

I want to point out that this does get some of the images saved correctly. I believe what I'm missing is a callback to make sure that the file has been successfully saved before moving on to the next item in the list. I have no idea how to implement that.

Another quick note (not important) is that I'm trying to download images from a Tableau Server using the REST api.

Answer Source

Just figured it out using the async module

async.eachSeries(views, (function(view, callback) {
            var thumbPath = 'public/images/thumbnails/' + req.session.SITE + '/views/' + req.session.views[view]['url'] + '.png'
                    url: 'http://' + SERVERURL + '/api/2.2/sites/' + req.session.siteID
                        + '/workbooks/' + req.session.views[view]['workbookID'] + '/views/' + req.session.views[view]['id'] + '/previewimage',
                    headers: {
                        'Content-Type': 'image/png',
                        'X-Tableau-Auth': req.session.authToken
                    console.log("a thumb failed to download")
                } else {
                    console.log("all thumbs downloaded")